Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit28d51ff

Browse files
authored
Create 0013 Roman to Integer
1 parent78f714d commit28d51ff

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

‎java/0013 Roman to Integer

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public int romanToInt(String s) {
3+
HashMap<Character, Integer> map = new HashMap();
4+
map.put('I', 1);
5+
map.put('V' ,5);
6+
map.put('X' ,10);
7+
map.put('L' ,50);
8+
map.put('C' ,100);
9+
map.put('D' ,500);
10+
map.put('M' ,1000);
11+
12+
int result = 0;
13+
for(int i=0; i < s.length(); i++){
14+
if (i > 0 && map.get(s.charAt(i)) > map.get(s.charAt(i-1))) {
15+
result += map.get(s.charAt(i)) - 2*map.get(s.charAt(i-1));
16+
} else {
17+
result += map.get(s.charAt(i));
18+
}
19+
}
20+
return result;
21+
}
22+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp