首页 > 其他 > 详细

Integer to Roman

时间:2015-05-26 02:10:58      阅读:226      评论:0      收藏:0      [点我收藏+]

Given a roman numeral, convert it to an integer.

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

?

public class Solution {
    public String intToRoman(int num) {
        Map<Integer, String> map = new HashMap<Integer, String>();
        map.put(1, "I");
        map.put(4, "IV");
        map.put(5, "V");
        map.put(9, "IX");
        map.put(10, "X");
        map.put(40, "XL");
        map.put(50, "L");
        map.put(90, "XC");
        map.put(100, "C");
        map.put(400, "CD");
        map.put(500, "D");
        map.put(900, "CM");
        map.put(1000, "M");
        int nums[] = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
        String res = "";
        for (int i = 0; i < nums.length; i++) {
        	int t = num/nums[i];
        	num = num%nums[i];
        	for (int j = 1; j <= t; j++) {
        		res += map.get(nums[i]);
        	}
        }
        return res;
    }
}

?

Integer to Roman

原文:http://hcx2013.iteye.com/blog/2213973

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!