【LeetCode】13. Roman to Integer
2017-06-21 09:03
274 查看
Description:
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
题目分析:
题目要求罗马数字转化为整型数字,查询罗马特殊字符:
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
Solution:
class Solution {
public:
int romanToInt(string s) {
int sum=0;
int strLen = s.size();
map<char,int> myMap;
myMap['I']=1;
myMap['V']=5;
myMap['X']=10;
myMap['L']=50;
myMap['C']=100;
myMap['D']=500;
myMap['M']=1000;
//vector<int> myVec(4,0);
int i=0;
int j;
while( i < strLen )
{
while(i<strLen&&myMap[s[i]] >= myMap[s[i+1]])
{
sum = sum + myMap[s[i]];
i++;
}
while(i<strLen&&myMap[s[i]] < myMap[s[i+1]])
{
sum = sum - myMap[s[i]];
i++;
}
}
return sum;
}
};
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
题目分析:
题目要求罗马数字转化为整型数字,查询罗马特殊字符:
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
Solution:
class Solution {
public:
int romanToInt(string s) {
int sum=0;
int strLen = s.size();
map<char,int> myMap;
myMap['I']=1;
myMap['V']=5;
myMap['X']=10;
myMap['L']=50;
myMap['C']=100;
myMap['D']=500;
myMap['M']=1000;
//vector<int> myVec(4,0);
int i=0;
int j;
while( i < strLen )
{
while(i<strLen&&myMap[s[i]] >= myMap[s[i+1]])
{
sum = sum + myMap[s[i]];
i++;
}
while(i<strLen&&myMap[s[i]] < myMap[s[i+1]])
{
sum = sum - myMap[s[i]];
i++;
}
}
return sum;
}
};
相关文章推荐
- Leetcode 13. Roman to Integer(python)
- [Leetcode] 13. Roman to Integer 解题报告
- LeetCode 13. Roman to Integer(罗马数字转阿拉伯数字)
- LeetCode 13. Roman to Integer
- 13. Roman to Integer--LeetCode Record
- Leetcode13. Roman to Integer
- [leetcode] 13. Roman to Integer[facebook](11)
- 【Leetcode】:13. Roman to Integer 问题 in JAVA
- leetcode-13. Roman to Integer
- leetCode 13. Roman to Integer 字符串
- LeetCode 13. Roman to Integer(java)
- leetcode 13. Roman to Integer
- LeetCode 13. Roman to Integer 罗马数字转整数
- 【LeetCode】(C#)13. Roman to Integer(Easy)
- [LeetCode]13. Roman to Integer
- leetcode_13. Roman to Integer
- LeetCode-13. Roman to Integer
- [leetcode]13. Roman to Integer
- leetcode--13. Roman to Integer
- 2017.10.21 LeetCode - 13. Roman to Integer - 28. Implement strStr()