Given an integer, convert it to a roman numeral.
==============================================
Key point:
建个二维数组来存数。
===================================================================
CODE:
public class Solution {
public String intToRoman(int num) {
String ret = "";
String [][] table = { {"","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"}};
int [] n = {1,10,100,1000};
int index = 3;
while(index >= 0){
int rem = num % n[index];
int dif = num / n[index];
ret += table[index][dif];
num = rem ;
index --;
}
return ret;
}
}
No comments:
Post a Comment