https://leetcode.com/problems/roman-to-integer/submissions/
풀이
class Solution {
public int romanToInt(String s) {
int pre = RomanToInteger(s.charAt(s.length()-1));
int answer = pre;
for(int i=s.length()-2; i>=0; i--){
int cur = RomanToInteger(s.charAt(i));
if(cur < pre ){
answer -= cur;
pre = 0;
} else {
answer += cur;
pre = cur;
}
}
return answer;
}
public int RomanToInteger(char c){
switch(c){
case 'I':
return 1;
case 'V':
return 5;
case 'X':
return 10;
case 'L':
return 50;
case 'C':
return 100;
case 'D':
return 500;
case 'M':
return 1000;
}
return 0;
}
}
'Leetcode 100문제 도전' 카테고리의 다른 글
[Leetcode 7/100] Increasing Order Search Tree - Easy (0) | 2020.08.26 |
---|---|
[Leetcode 6/100] Same Tree - Easy (0) | 2020.08.25 |
[Leetcode 4/100] Palindrome Number - Easy (1) | 2020.06.28 |
[Leetcode 3/100] Reverse Integer - Easy (1) | 2020.06.17 |
[Leetcode 2/100] Add Two Numbers - Medium (0) | 2020.06.16 |