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

Commit9161e0b

Browse files
authored
Merge pull requestneetcode-gh#2287 from n0nchalant/patch-2
java/0013-roman-to-integer
2 parents919331c +ec0d31b commit9161e0b

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

‎java/0013-roman-to-integer.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
classSolution {
2+
publicintromanToInt(Strings) {
3+
HashMap<Character,Integer>map =newHashMap();
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+
intresult =0;
13+
for(inti=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+
returnresult;
21+
}
22+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp