Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 3fd1293

Browse files
committed
No13.java
1 parent a155b76 commit 3fd1293

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ I really **enjoy** it, I will always update it.
5555
| ------------------------------------------------------------ | ------------------------------------------------------------ | ---- | ---------- | ---- |
5656
| [7.Reverse Integer](https://leetcode.com/problems/reverse-integer) | [java](https://github.com/tujietg/leetcode/blob/master/math/No07.java) | | Easy | |
5757
| [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 | |
5859

5960

6061

math/No13.java

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
}

0 commit comments

Comments
 (0)