Skip to content

Commit 69d3c11

Browse files
authored
Add 013_roman_to_integer file in java (qiyuangong#54)
* Roman in table Contributed by @naveen701526
1 parent 250a8c7 commit 69d3c11

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

java/013_Roman_to_Integer.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public int romanToInt(String s) {
3+
int[] arr = new int['A' + 26];
4+
arr['I'] = 1;
5+
arr['V'] = 5;
6+
arr['X'] = 10;
7+
arr['L'] = 50;
8+
arr['C'] = 100;
9+
arr['D'] = 500;
10+
arr['M'] = 1000;
11+
12+
int result = 0;
13+
int prev = 0;
14+
15+
for (int i = s.length() - 1; i >= 0; i--) {
16+
int current = arr[s.charAt(i)];
17+
result += prev > current ? -current : current;
18+
prev = current;
19+
}
20+
21+
return result;
22+
}
23+
}

0 commit comments

Comments
 (0)