【LeetCode从零单刷】Roman to Integer
来源:互联网 发布:翻 墙软件 编辑:程序博客网 时间:2024/06/05 11:06
题目:
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
解答:
说实话这题目就是欺负中国人不了解罗马数字。可以参考维基百科中相关介绍:传送门。其中有性质如下:
之后的事情就是查表,并且不考虑进制问题,一位一位的计数。
比较与右侧字母的大小关系,如果大于等于右侧字母则总和加上这个字母;否则减去这个字母。
class Solution {public: int romanToInt(string s) { std::map<char, int>table; table['I'] = 1; table['V'] = 5; table['X'] = 10; table['L'] = 50; table['C'] = 100; table['D'] = 500; table['M'] = 1000; int len = s.size(); int ans = 0; for(int i = 0; i<len - 1; i++) { if(table[s[i]] < table[s[i+1]]) ans = ans - table[s[i]]; else ans = ans + table[s[i]]; } ans += table[s[len - 1]]; return ans; }};
0 0
- 【LeetCode从零单刷】Roman to Integer
- 【LeetCode从零单刷】Integer to Roman
- LeetCode Roman to Integer && Integer to Roman
- 【leetcode】Roman to Integer | Integer to Roman
- Leetcode - "Roman to Integer" & "Integer to Roman"
- LeetCode:Roman to Integer,Integer to Roman
- LeetCode Integer to Roman & Roman to Integer
- leetcode - roman to integer & integer to roman
- LeetCode:Roman to Integer
- LeetCode: Integer to Roman
- 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: Integer to Roman
- Leetcode: Roman to Integer
- ABAP程序编写过程中怎么提高程序执行效率一
- Intent(1)
- openfire 安全模式登陆
- Use Static Method in Powershell
- SQLserver 查看表详细信息--删除重复数据
- 【LeetCode从零单刷】Roman to Integer
- linux服务器开户:groupadd, useradd, passwd的使用
- java集合框架---泛型总结
- JAVA和Scala写文件类FileWriter/FileOutputStream/BufferedWriter
- FZU2136--取糖果 (线段树+RMQ)
- 快速幂模板(数+矩阵)
- phongap 命令行签名,apk签名成功,但是安装时提示包解析错误
- POJ2752 Seek the Name, Seek the Fame KMP-next数组的应用
- 二分匹配_HDU_1151