<LeetCode OJ> 13 / 12 Roman to Integer & Integer to Roman
2015-12-18 23:01
513 查看
13. Roman to Integer
Question
Total Accepted: 64343 Total
Submissions: 175540 Difficulty: Easy
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
本题需要额外知道罗马数字基本知识:
记数方法
1、相同的数字连写,所表示的数等于这些数字相加得到的数,如:Ⅲ = 3;
2、小的数字在大的数字的右边,所表示的数等于这些数字相加得到的数, 如:Ⅷ = 8;Ⅻ = 12;
3、小的数字,(限于Ⅰ、X 和C)在大的数字的左边,所表示的数等于大数减小数得到的数,如:Ⅳ= 4;Ⅸ= 9;
4、正常使用时,连写的数字重复不得超过三次。(表盘上的四点钟“IIII”例外)
5、在一个数的上面画一条横线,表示这个数扩大1000倍。
分析:DDONEONE
一个罗马字符对应一个数字权,那么就可以用数据结构map
对于s中出现的每一个罗马字符从右到左(无低位高位这么一说),如果权值在变大,则权相加
如果变小了,则减去这个权值
12. Integer to Roman
Question
Total Accepted: 50410 Total
Submissions: 139575 Difficulty: Medium
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
思路首先:DONE
反复以较大权作为判断标准,当前这个数是否大于最大的权?若大于则循环判断有几个这样的权?若小于,则降低权进行判断,重复判断操作
注:本博文为EbowTang原创,后续可能继续更新本文。如果转载,请务必复制本条信息!
原文地址:http://blog.csdn.net/ebowtang/article/details/50354684
原作者博客:http://blog.csdn.net/ebowtang
参考资源:
【1】第一题的参考:http://blog.csdn.net/jellyyin/article/details/13165731
【2】第二题的参考:http://blog.csdn.net/beiyeqingteng/article/details/8547565
Question
Total Accepted: 64343 Total
Submissions: 175540 Difficulty: Easy
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
本题需要额外知道罗马数字基本知识:
基本字符 | I | V | X | L | C | D | M |
相应的阿拉伯数字表示为 | 1 | 5 | 10 | 50 | 100 | 500 | 1000 |
2、小的数字在大的数字的右边,所表示的数等于这些数字相加得到的数, 如:Ⅷ = 8;Ⅻ = 12;
3、小的数字,(限于Ⅰ、X 和C)在大的数字的左边,所表示的数等于大数减小数得到的数,如:Ⅳ= 4;Ⅸ= 9;
4、正常使用时,连写的数字重复不得超过三次。(表盘上的四点钟“IIII”例外)
5、在一个数的上面画一条横线,表示这个数扩大1000倍。
分析:DDONEONE
一个罗马字符对应一个数字权,那么就可以用数据结构map
对于s中出现的每一个罗马字符从右到左(无低位高位这么一说),如果权值在变大,则权相加
如果变小了,则减去这个权值
class Solution { public: int romanToInt(string s) { int result=0; map<char,int> roman; roman['I']=1; roman['V']=5; roman['X']=10; roman['L']=50; roman['C']=100; roman['D']=500; roman['M']=1000; for(int i=s.size()-1;i>=0;i--) { if(i==s.size()-1) result+=roman[s[i]]; else if(roman[s[i]] >= roman[s[i+1]]) result+=roman[s[i]]; else result-=roman[s[i]]; } return result; } };
12. Integer to Roman
Question
Total Accepted: 50410 Total
Submissions: 139575 Difficulty: Medium
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
思路首先:DONE
反复以较大权作为判断标准,当前这个数是否大于最大的权?若大于则循环判断有几个这样的权?若小于,则降低权进行判断,重复判断操作
class Solution { public: string intToRoman(int num) {//两个基数组,可形成3999内任意整数 int values[13] = {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]); //append函数是向string 的后面追加字符或字符串。或者result+=numerals[i]; } } return result; } };
注:本博文为EbowTang原创,后续可能继续更新本文。如果转载,请务必复制本条信息!
原文地址:http://blog.csdn.net/ebowtang/article/details/50354684
原作者博客:http://blog.csdn.net/ebowtang
参考资源:
【1】第一题的参考:http://blog.csdn.net/jellyyin/article/details/13165731
【2】第二题的参考:http://blog.csdn.net/beiyeqingteng/article/details/8547565
相关文章推荐
- 书评:《算法之美( Algorithms to Live By )》
- 动易2006序列号破解算法公布
- C#数据结构之顺序表(SeqList)实例详解
- Ruby实现的矩阵连乘算法
- C#插入法排序算法实例分析
- Lua教程(七):数据结构详解
- 解析从源码分析常见的基于Array的数据结构动态扩容机制的详解
- 超大数据量存储常用数据库分表分库算法总结
- C#数据结构与算法揭秘二
- C#冒泡法排序算法实例分析
- C#数据结构之队列(Quene)实例详解
- C#数据结构揭秘一
- C#数据结构之单链表(LinkList)实例详解
- 算法练习之从String.indexOf的模拟实现开始
- C#算法之关于大牛生小牛的问题
- C#实现的算24点游戏算法实例分析
- c语言实现的带通配符匹配算法
- 数据结构之Treap详解
- 浅析STL中的常用算法
- 算法之排列算法与组合算法详解