[C++]LeetCode 12: Integer to Roman(将整数转换为罗马数字)
2015-05-19 21:08
716 查看
Problem:
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
分析:
题目的要点在于罗马数字规则,见百度百科中罗马数字词条。
剩下的就是写出与规则对应的逻辑。
AC Code(C++):
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
分析:
题目的要点在于罗马数字规则,见百度百科中罗马数字词条。
剩下的就是写出与规则对应的逻辑。
AC Code(C++):
class Solution { public: //3999 / 3999 test cases passed. //Runtime: 58 ms string intToRoman(int num) { if (num > 3999) { return ""; } string BitNum[10] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}; string TenNum[10] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}; string HunNum[10] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}; string ThuNum[4] = {"", "M", "MM", "MMM"}; string result; int divNum = 1000; while (divNum) { int index = num / divNum; switch (divNum) { case 1000: result += ThuNum[index]; break; case 100: result += HunNum[index]; break; case 10: result += TenNum[index]; break; case 1: result += BitNum[index]; break; default: break; } if (divNum == 1) { break; } else{ num %= divNum; divNum /= 10; } } return result; } };
相关文章推荐
- leetcode-12. Integer to Roman(整数转换成罗马数字)
- LeetCode 12 Integer to Roman (整数转罗马数字)
- LeetCode | Integer to Roman(整数转换成罗马数字)
- leetCode 12.Integer to Roman (整数转罗马数字) 解题思路和方法
- leetcode 12 Integer to Roman(整型数转换为罗马数字)
- [LeetCode]12. Integer to Roman(整数转化为罗马数字)
- LeetCode 12. Integer to Roman(阿拉伯转罗马数字)
- [LeetCode] Integer to Roman 整数转化成罗马数字
- 【LeetCode-面试算法经典-Java实现】【013-Roman to Integer (罗马数字转成整数)】
- [LeetCode] Roman to Integer 罗马数字转化成整数
- Leetcode 13 Roman to Integer 罗马数字转整数
- (LeetCode) Roman to Integer --- 罗马数字转整数
- LeetCode 12. Integer to Roman(罗马字符转阿拉伯数字)
- leetcode 12 ,13 Integer to Roman &&Roman to Integer 罗马与阿拉伯数组转换
- [LeetCode] Roman to Integer 罗马数字转整数
- Leet Code 12 Integer to Roman - 整数转成罗马数字 - Java
- Integer to Roman (罗马数字转换) 【leetcode】
- LeetCode--Roman to Integer 罗马数字转化成整数
- [Leetcode] Roman to integer 罗马数字转成整数
- 58. 分析、测试与总结:罗马数字和阿拉伯数字的转换[roman to integer and integer to roman in c++]