leetcode.com/problems/roman-to-integer/
class Solution {
public:
int romanToInt(string s) {
int result = 0;
for(int i=0; i<s.size(); i++){
if(s.at(i) == 'I'){
if(i != s.size()-1 && s.at(i+1) == 'V' ){
result += 4;
i++;
}
else if(i != s.size()-1 && s.at(i+1) == 'X' ){
result += 9;
i++;
}
else{
result += 1;
}
}
else if(s.at(i) == 'V'){
result += 5;
}
else if(s.at(i) == 'X'){
if(i != s.size()-1 && s.at(i+1) == 'L' ){
result += 40;
i++;
}
else if(i != s.size()-1 && s.at(i+1) == 'C' ){
result += 90;
i++;
}
else{
result += 10;
}
}
else if(s.at(i) == 'L'){
result += 50;
}
else if(s.at(i) == 'C'){
if(i != s.size()-1 && s.at(i+1) == 'D' ){
result += 400;
i++;
}
else if(i != s.size()-1 && s.at(i+1) == 'M' ){
result += 900;
i++;
}
else{
result += 100;
}
}
else if(s.at(i) == 'D'){
result += 500;
}
else{
result += 1000;
}
}
return result;
}
};
'알고리즘 문제풀이 > LeetCode' 카테고리의 다른 글
LeetCode 844번 - Backspace String Compare (0) | 2020.07.30 |
---|---|
LeetCode 686번- Repeated String Match (0) | 2020.07.28 |
LeetCode 67번 - Add Binary (0) | 2020.07.28 |
LeetCode 14번 - Longest Common Prefix (0) | 2020.07.28 |
LeetCode 81번 - Search in Rotated Sorted Array II (0) | 2020.07.27 |