【LeetCode】11. Integer to Roman
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
仔细理解罗马数的规则,把IV IX等也作为基本元素列入对应表中。然后每次选择能表示的最大值,把对应的字符串连起来。直到剩下的数=0为止。
public class Solution {
public String intToRoman(int num) {
// Note: The Solution object is instantiated only once and is reused by each test case.
String str="";
String symbol[] = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
int value[] = {1000,900,500,400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
for(int i=0;num!=0;++i)
{
while(num>=value[i])
{
num-=value[i];
str+=symbol[i];
}
}
return str;
}
}

浙公网安备 33010602011771号