Integer to Roman ——解题报告
来源:互联网 发布:数据库笔试题 编辑:程序博客网 时间:2024/06/04 00:39
【题目】
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
分析:
首先,我们需要知道罗马数字的表示方法,可参考链接:http://blog.csdn.net/ljiabin/article/details/39968583
然后,根据罗马数字的表示方法,我们可以把输入的num分成每一位,对应位转换成罗马数字即可。
具体实现的时候,我们可以把用到的罗马数字单位做成一个table,查找即可。
代码:
class Solution {public: string intToRoman(int num) { string table[4][10] = { {"", "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"} }; string res = ""; int digit = 0; while(num) { res = table[digit][num%10] + res; // 查表,需要指明行列 digit++; // 下一位 num /= 10; } return res; }};
1 0
- Integer to Roman ——解题报告
- Roman to Integer ——解题报告
- 【LeetCode】Integer to Roman 和 Roman to Integer 解题报告
- 【LeetCode】Integer to Roman 和 Roman to Integer 解题报告
- [LeetCode]Roman to Integer,解题报告
- [LeetCode]Integer to Roman,解题报告
- LeetCode解题报告--Integer to Roman
- LeetCode解题报告--Roman to Integer
- 【LeetCode】Roman to Integer 解题报告
- 【LeetCode】Integer to Roman 解题报告
- [leetcode] 13. Roman to Integer 解题报告
- [LeetCode] Integer to Roman 解题报告
- [LeetCode] Roman To Integer 解题报告
- [leetcode] 12. Integer to Roman 解题报告
- LeetCode 13. Roman to Integer 解题报告
- LeetCode 12. Integer to Roman 解题报告
- [Leetcode] 12. Integer to Roman 解题报告
- [Leetcode] 13. Roman to Integer 解题报告
- Sql Server 列转行 Pivot使用
- 关于Android资源的使用
- linux下查找文件的方法
- Transact-SQL中使用PATINDEX和STUFF替换字串中非数字字符
- 影像质量评价指标汇总
- Integer to Roman ——解题报告
- 单例模式
- 类模板——类模板的特化问题
- Android---36---读取SD卡内容
- C#中的值传递和引用传递详细解析
- Codeforces 383 A. Milking cows(贪心)
- linux下查看cpu,内存,硬盘等硬件信息的方法
- Android关键资源
- Maximum Depth of Binary Tree -- leetcode