码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode#12Integer to Roman

时间:2015-05-23 06:33:14      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:interview   convert   question   within   

Integer to Roman

 Total Accepted: 31922 Total Submissions: 91429My Submissions

Question Solution 


Given an integer, convert it to a roman numeral.

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


Show Tags

Have you met this question in a real interview? 

Yes

 

No

Discuss


【题目】

Given a roman numeral, convert it to an integer. Or, Given an integer, convert it to a roman numeral.

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

【罗马数字】

1~9: {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};

10~90: {"X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};

100~900: {"C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};

1000~3000: {"M", "MM", "MMM"}.


public class Solution {

    public String intToRoman(int num) {

        String[][] roman = {  

            {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"},  

            {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"},  

            {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"},  

            {"", "M", "MM", "MMM"}  

        };  

          

        String ret = "";  

        int digit = 0;  

        while (num != 0) {  

            int remain = num % 10;  

            ret = roman[digit][remain] + ret;  

            digit++;  

            num /= 10;  

        }  

          

        return ret;  

    }

}


Leetcode#12Integer to Roman

标签:interview   convert   question   within   

原文地址:http://7061299.blog.51cto.com/7051299/1654083

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!