[LeetCode] Integer to Roman, problem solving report

Source: Internet
Author: User

Preface this weekend, plus the five days of this week, I need to help my mentor to build a project that is ending with the bad news of beiyou. It is very hard, but the kindness of Zhiyu is indeed too great. I can only do my best.
However, the project is controlled by the svn version library, and the submission record of github is blank for another day. It seems quite uncomfortable. If I don't submit code on github one day, I must submit it on svn.
Question Given an integer, convert it to a roman numeral.

Input is guaranteed to be within the range from 1 to 3999.

Thinking of the title is not difficult, is less than 3999 of Arabic numerals into the Roman numerals, there was an article written before the characteristics of the Roman numerals, see the link: http://blog.csdn.net/wzy_1988/article/details/17057929
Taking 3849 as an example, we only need to consider the number of each digit. For each digit, digit is divided into the following situations: digit = 00 <digit <= 3 digit = 45 <= digit <= 9 digit = 9 for each digit, You can process these five conditions separately.
AC code

public class Solution {    public String intToRoman(int num) {        StringBuilder result = new StringBuilder();        char[] roman = {'I', 'V', 'X', 'L', 'C', 'D', 'M'};                int digit, base = 1000;                for (int i = roman.length + 1; i >= 0 && num > 0; i -= 2, base /= 10) {            digit = num / base;                        if (digit == 0) {                continue;            } else if (digit <= 3) {                for (int j = 0; j < digit; j ++) {                    result.append(roman[i - 2]);                }            } else if (digit == 4) {                result.append(roman[i - 2]);                result.append(roman[i - 1]);            } else if (digit <= 8) {                result.append(roman[i- 1]);                for (int j = digit - 5; j > 0; j --) {                    result.append(roman[i - 2]);                }            } else if (digit == 9) {                result.append(roman[i - 2]);                result.append(roman[i]);            }                        num = num % base;        }                        return result.toString();    }}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.