Leetcode Roman to Integer
来源:互联网 发布:python gnureadline 编辑:程序博客网 时间:2024/06/08 07:13
罗马数转整数,比较新鲜。
罗马数的记数方法和组数规则可以参考http://baike.baidu.com/link?url=mYiSRssk7gcAkxAsio8k0tfjy7dZV8PYMYvpW28rLGxfLJimaoIibKS_WPTY2KEy
理解了以后再做就简单了不少。
class Solution {public: bool judge(string s, int i){ if((s[i] == 'V' || s[i] == 'X' )&& s[i-1] == 'I') return true; else if((s[i] == 'L' || s[i] == 'C') && s[i-1] == 'X') return true; else if((s[i] == 'M' || s[i] == 'D') && s[i-1] == 'C') return true; return false; } int romanToInt(string s) { // Note: The Solution object is instantiated only once and is reused by each test case. int ans = 0; if(s.empty()) return ans; int r2i[255]; r2i['I'] = 1, r2i['V'] = 5, r2i['X'] = 10, r2i['L'] = 50, r2i['C'] = 100, r2i['D'] = 500, r2i['M'] = 1000; for(int i = s.size()-1; i >= 0;){ if(i == 0){ ans += r2i[s[i]]; break; } if(judge(s, i)){ ans += (r2i[s[i]]-r2i[s[i-1]]); i -= 2; } else{ ans += r2i[s[i]]; i--; } } return ans; }};
- 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
- LeetCode Integer to Roman
- 【leetcode】Roman to Integer
- friso-1.4.0 发布 - 英文同义词追加+中英任意组合识别+小数单位识别
- Android之Activity
- C#反射读取类定制特性信息
- centos6文本模式更新为图形模式
- GH的热键,只要用gh都要拜读一下吧
- Leetcode Roman to Integer
- jcseg-1.9.0 发布 - 英文同义词追加+中英任意组合识别
- Linux就是这个范儿之第一次亲密接触(1)
- Spring, Hibernate, Maven, and Struts2 integration tutorial
- WINCE5.0上USB设备的自动检测
- About IndexDB
- 基于双队列实现huffman树
- fromImageReader的用法
- 看看你会几个?9大最火的移动开发技能