leetcode:Roman to Integer
来源:互联网 发布:jira mysql驱动 win7 编辑:程序博客网 时间:2024/06/03 19:06
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
Subscribe to see which companies asked this question
class Solution { int getNum(char c) { if (c == 'M') return 1000; if (c == 'D') return 500; if (c == 'C') return 100; if (c == 'L') return 50; if (c == 'X') return 10; if (c == 'V') return 5; if (c == 'I') return 1; } public: int romanToInt(string s) { int total = 0; int curNum = 0; int preNum = 0; int count = 0; for (int i=0; i<s.size(); i++) { curNum = getNum(s[i]); if (curNum == preNum) { count++; total += curNum; } else { if (preNum != 0 && curNum > preNum) { total = total-2*preNum*count; } total += curNum; preNum = curNum; count = 1; } } return total; }};
0 0
- 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
- centos6.5集群lvs+keepalived部署
- windows编程基础篇 -- 消息
- NSUserDefaults的使用
- UICollectionView添加Header Footer没被调用
- 快速查看Mac下软件的所有快捷键— CheatSheet
- leetcode:Roman to Integer
- 继承AppCompatActivity如何实现无标题
- Rational Rose Enterprise Edition安装使用
- 学习笔记5
- leetcode 160:Intersection of Two Linked Lists
- java如何将一个序列化对象添加到压缩文件中
- 使用ServerSocketChannel与SocketChannel实现的CS模式
- UI基础-05 HelloWorld 工程中的故事板文件
- xwiki 7.14 Windows安装教程(jetty + MySQL5.7+jdk1.7)