leetcode题解||Integer to Roman问题
2015-03-18 16:11
441 查看
problem:
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
将1-3999的整数转换为罗马数字
thinking:
(1)
对照举例
个位数举例
Ⅰ,1 】Ⅱ,2】 Ⅲ,3】 Ⅳ,4 】Ⅴ,5 】Ⅵ,6】Ⅶ,7】 Ⅷ,8 】Ⅸ,9 】
十位数举例
Ⅹ,10】 Ⅺ,11 】Ⅻ,12】 XIII,13】 XIV,14】 XV,15 】XVI,16 】XVII,17 】XVIII,18】 XIX,19】 XX,20】 XXI,21 】XXII,22 】XXIX,29】 XXX,30】 XXXIV,34】 XXXV,35 】XXXIX,39】 XL,40】 L,50 】LI,51】 LV,55】 LX,60】 LXV,65】 LXXX,80】 XC,90 】XCIII,93】 XCV,95 】XCVIII,98】 XCIX,99 】
百位数举例
C,100】 CC,200 】CCC,300 】CD,400】 D,500 】DC,600 】DCC,700】 DCCC,800 】CM,900】 CMXCIX,999】
千位数举例
M,1000】 MC,1100 】MCD,1400 】MD,1500 】MDC,1600 】MDCLXVI,1666】 MDCCCLXXXVIII,1888 】MDCCCXCIX,1899 】MCM,1900 】MCMLXXVI,1976】 MCMLXXXIV,1984】 MCMXC,1990 】MM,2000 】MMMCMXCIX,3999】
(2)避免N多条件判断的技巧:利用罗马数字自身的规律,减去一个基数,来简化复杂度
code:
class Solution {
public:
string intToRoman(int num)
{
int values[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };
string numerals[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
string result;
for (int i = 0; i < 13; i++) {
while (num >= values[i]) {
num -= values[i];
result.append(numerals[i]);
}
}
return result;
}
};
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
将1-3999的整数转换为罗马数字
thinking:
(1)
对照举例
个位数举例
Ⅰ,1 】Ⅱ,2】 Ⅲ,3】 Ⅳ,4 】Ⅴ,5 】Ⅵ,6】Ⅶ,7】 Ⅷ,8 】Ⅸ,9 】
十位数举例
Ⅹ,10】 Ⅺ,11 】Ⅻ,12】 XIII,13】 XIV,14】 XV,15 】XVI,16 】XVII,17 】XVIII,18】 XIX,19】 XX,20】 XXI,21 】XXII,22 】XXIX,29】 XXX,30】 XXXIV,34】 XXXV,35 】XXXIX,39】 XL,40】 L,50 】LI,51】 LV,55】 LX,60】 LXV,65】 LXXX,80】 XC,90 】XCIII,93】 XCV,95 】XCVIII,98】 XCIX,99 】
百位数举例
C,100】 CC,200 】CCC,300 】CD,400】 D,500 】DC,600 】DCC,700】 DCCC,800 】CM,900】 CMXCIX,999】
千位数举例
M,1000】 MC,1100 】MCD,1400 】MD,1500 】MDC,1600 】MDCLXVI,1666】 MDCCCLXXXVIII,1888 】MDCCCXCIX,1899 】MCM,1900 】MCMLXXVI,1976】 MCMLXXXIV,1984】 MCMXC,1990 】MM,2000 】MMMCMXCIX,3999】
(2)避免N多条件判断的技巧:利用罗马数字自身的规律,减去一个基数,来简化复杂度
code:
class Solution {
public:
string intToRoman(int num)
{
int values[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };
string numerals[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
string result;
for (int i = 0; i < 13; i++) {
while (num >= values[i]) {
num -= values[i];
result.append(numerals[i]);
}
}
return result;
}
};
相关文章推荐
- leetcode题解 || Roman to Integer问题
- [LeetCode]题解(python):013-Roman to Integer
- LeetCode题解——Roman to Integer
- LeetCodet题解--12. Integer to Roman
- leetcode_12题——Integer to Roman(string,数学问题)
- leetcode题解-12. Integer to Roman
- LeetCode题解:Roman to Integer
- 013_LeetCode_13 Roman to Integer 题解
- 【LeetCode解答六】Integer to Roman问题Java解答
- leetcode题解-13. Roman to Integer && 14. Longest Common Prefix && 20. Valid Parentheses
- LeetCode题解 #12 Integer to Roman
- LeetCode题解——Roman to Integer
- leetcode_13题——Roman to Integer(string,数学问题)
- [LeetCode]题解(python):012-Integer to Roman
- 12. Integer to Roman LeetCode题解
- LeetCode题解——Integer to Roman
- LeetCode题解——Integer to Roman
- 【LeetCode解答七】Roman to Integer问题Java解答
- LeetCode题解-12-Integer to Roman
- leetcode解决问题的方法||Integer to Roman问题