LeetCoder 13. Roman to Integer
来源:互联网 发布:小说书籍知乎 编辑:程序博客网 时间:2024/06/02 02:37
题意
将一串罗马数字转换为阿拉伯数字,刚好是上一道题的颠倒:Integer to Roman
思路
根据罗马数字的规则,发现只要是小的在大的左边的都是大的减小的,小的在大的右边的,都是大的加小的,根据这个规律,遍历字符串即可.
结果
Your runtime beats 15.56 % of cpp submissions.
代码
class Solution {public: map<char, int>mp; int romanToInt(string s) { mp['I'] = 1; mp['V'] = 5; mp['X'] = 10; mp['L'] = 50; mp['C'] = 100; mp['D'] = 500; mp['M'] = 1000; size_t len = s.length(); int ans = 0; for(size_t i = 0; i < len - 1 ;i++){ if(mp[s[i]] < mp[s[i + 1]]){ ans -= mp[s[i]]; } else{ ans += mp[s[i]]; } } ans += mp[s[len - 1]]; return ans; }};
0 0
- LeetCoder 13. Roman to Integer
- LeetCoder 12. Integer to Roman
- 13. Roman to Integer
- 13.Roman to Integer
- 13. Roman to Integer
- 13.Roman to Integer
- 13. Roman to Integer
- 13. Roman to Integer
- 13. Roman to Integer
- 13. Roman to Integer
- 13. Roman to Integer
- 13. Roman to Integer
- 13. Roman to Integer
- 13. Roman to Integer
- 13. Roman to Integer
- 13. Roman to Integer
- 13. Roman to Integer
- 13. Roman to Integer
- shell变量
- xdoj 1142: 删除字符
- 并查集模板
- Hadoop安装教程
- 最短路径模板
- LeetCoder 13. Roman to Integer
- Java 实例
- Hive中如何快速的复制一张分区表(包括数据)
- java多态polymorphic多态的理解一
- NPOI设置Excel单元格样式
- ISP图像质量调节介绍
- Linux--shell编程学习
- Linux线程------函数用法详解
- python pandas更新