File tree 2 files changed +21
-0
lines changed
2 files changed +21
-0
lines changed Original file line number Diff line number Diff line change @@ -55,6 +55,7 @@ I really **enjoy** it, I will always update it.
55
55
| ------------------------------------------------------------ | ------------------------------------------------------------ | ---- | ---------- | ---- |
56
56
| [ 7.Reverse Integer] ( https://leetcode.com/problems/reverse-integer ) | [ java] ( https://github.com/tujietg/leetcode/blob/master/math/No07.java ) | | Easy | |
57
57
| [ 9.Palindrome Number] ( https://leetcode.com/problems/palindrome-number ) | [ java] ( https://github.com/tujietg/leetcode/blob/master/math/No09.java ) | O(N) | Easy | |
58
+ | [ 13.Roman to Integer] ( https://leetcode.com/problems/roman-to-integer ) | [ java] ( https://github.com/tujietg/leetcode/blob/master/math/No13.java ) | O(N) | Easy | |
58
59
59
60
60
61
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int romanToInt (String s ) {
3
+ int sum = 0 ;
4
+ if (s .indexOf ("IV" ) != -1 || s .indexOf ("IX" ) != -1 ){sum -= 2 ;}
5
+ if (s .indexOf ("XL" ) != -1 || s .indexOf ("XC" ) != -1 ){sum -= 20 ;}
6
+ if (s .indexOf ("CD" ) != -1 || s .indexOf ("CM" ) != -1 ){sum -= 200 ;}
7
+ char [] ch = s .toCharArray ();
8
+
9
+ for (int i = 0 ; i < ch .length ; i ++){
10
+ if (ch [i ] == 'I' ){sum += 1 ;}
11
+ if (ch [i ] == 'V' ){sum += 5 ;}
12
+ if (ch [i ] == 'X' ){sum += 10 ;}
13
+ if (ch [i ] == 'L' ){sum += 50 ;}
14
+ if (ch [i ] == 'C' ){sum += 100 ;}
15
+ if (ch [i ] == 'D' ){sum += 500 ;}
16
+ if (ch [i ] == 'M' ){sum += 1000 ;}
17
+ }
18
+ return sum ;
19
+ }
20
+ }
You can’t perform that action at this time.
0 commit comments