LeetCode: Roman to Integer
2016-03-18 16:44
232 查看
13. Roman to Integer
Total Accepted: 77116 Total Submissions: 199727 Difficulty: EasyGiven 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) { int map[26]; char romanChar[] = { 'I', 'V', 'X', 'L', 'C', 'D', 'M' }; map['I' - 'A'] = 1; map['V' - 'A'] = 5; map['X' - 'A'] = 10; map['L' - 'A'] = 50; map['C' - 'A'] = 100; map['D' - 'A'] = 500; map['M' - 'A'] = 1000; int res = 0, tmp = 0; for (int i = 0; i < s.size(); i++) { if (map[s[i] - 'A'] > tmp) { res = res + map[s[i] - 'A'] - tmp * 2; } else { res = res + map[s[i] - 'A']; } tmp = map[s[i] - 'A']; } return res; } };
相关文章推荐
- 插入排序
- 【Linux】shell字符串分割、数组访问、条件判断
- android RecyclerView 实现瀑布流带图库浏览效果
- JS 利用appendChild对<li>标签进行排序
- Retrofit的分析以及实现步骤
- 如何在Linux下创建新的交换空间swap的方法
- Linux进程通信-信号学习
- 搜狐设计师:我从产品经理转为交互设计师的经验总结
- HashMap实现原理分析
- 用三种方式取出给定字符串中的目标字串
- 奖券数目
- 第四周项目1-求最大公约数(1)
- android studio改变主题字体大小
- iOS 随记
- Oracle学习笔记2 ---- 连接数据库
- 【实例】 python数据库编程
- 前端优化建议
- Java多线程系列--“基础篇”07之 线程休眠
- 解决Code First MySql数据库 Specified key was too long; max key length is 767 bytes异常
- C++虚继承(二) --- C++ 对象的内存布局(上)(陈皓)