LeetCode----- 12.Integer to Roman

Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.

解題思路:直接對輸入的num,直接求出各位上的數。

class Solution {
    public String intToRoman(int num) {
    	Map<Integer,String> map = new HashMap<Integer,String>(){
    		{
    			put(0,"");
    			put(1,"I");put(2,"II");put(3,"III");put(4,"IV");put(5,"V");
    			put(6,"VI");put(7,"VII");put(8,"VIII");put(9,"IX");
    			put(10,"X");put(20,"XX");put(30,"XXX");put(40,"XL");put(50,"L");
    			put(60,"LX");put(70,"LXX");put(80,"LXXX");put(90,"XC");
    			put(100,"C");put(200,"CC");put(300,"CCC");put(400,"CD");put(500,"D");
    			put(600,"DC");put(700,"DCC");put(800,"DCCC");put(900,"CM");
    			put(1000,"M");put(2000,"MM");put(3000,"MMM");
    		}
    	};
    	int q = num/1000;
    	int b = num%1000/100;
    	int s = num%100/10;
    	int g = num % 10;

    	return map.get(q*1000)+map.get(b*100)+map.get(s*10)+map.get(g);
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章