LeetCode 13: Roman to Integer
Given a roman numeral, convert it to an integer.3999 / 3999 test cases passed.
代码
C语言版本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| int romanCharToInt(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; default: return 0; } } int romanToInt(char* s) { int res = 0; int size = strlen(s); for (int i = 0; i < size; i++) { if (romanCharToInt(s[i]) < romanCharToInt(s[i + 1])){ res -= romanCharToInt(s[i]); } else { res += romanCharToInt(s[i]); } } return res; }
|
3999 / 3999 test cases passed.
Status: Accepted
Runtime: 24 ms
C++版本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| class Solution { public: int romanToInt(string s) { int num = 0; int size = s.size(); for (int i = 0; i < size; i++) { if (i < (size - 1) && romanCharToInt(s[i]) < romanCharToInt(s[i + 1])) { num -= romanCharToInt(s[i]); } else { num += romanCharToInt(s[i]); } } return num; } int romanCharToInt(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; default: return 0; } } };
|
3999 / 3999 test cases passed.
Status: Accepted
Runtime: 36 ms
java版本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| public class Solution { public int romanToInt(String s) { if(s == null || s.length() == 0) return 0; int len = s.length(); HashMap<Character,Integer> map = new HashMap<Character,Integer>(); map.put('I',1); map.put('V',5); map.put('X',10); map.put('L',50); map.put('C',100); map.put('D',500); map.put('M',1000); int result = map.get(s.charAt(len -1)); int pivot = result; for(int i = len -2; i>= 0;i--){ int cur = map.get(s.charAt(i)); if(cur >= pivot){ result += cur; }else{ result -= cur; } pivot = cur; } return result; } }
|
3999 / 3999 test cases passed.
Status: Accepted
Runtime: 24 ms