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

Commitb27739d

Browse files
author
applewjg
committed
Roman to Integer
Change-Id: Ib0dfb4dfb8cc9c6663d9483eb2ec71aae595101a
1 parent0b362be commitb27739d

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

‎RomantoInteger.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
Author: King, wangjingui@outlook.com
3+
Date: Dec 20, 2014
4+
Problem: Roman to Integer
5+
Difficulty: Easy
6+
Source: https://oj.leetcode.com/problems/roman-to-integer/
7+
Notes:
8+
Given a roman numeral, convert it to an integer.
9+
Input is guaranteed to be within the range from 1 to 3999.
10+
11+
Solution: use <map> or <unordered_map> (clean)
12+
*/
13+
publicclassSolution {
14+
publicintromanToInt(Strings) {
15+
HashMap<Character,Integer>map=newHashMap<Character,Integer>();
16+
map.put('M',1000);
17+
map.put('D',500);
18+
map.put('C',100);
19+
map.put('L',50);
20+
map.put('X',10);
21+
map.put('V',5);
22+
map.put('I',1);
23+
intres =0;
24+
for (inti =0;i <s.length(); ++i) {
25+
if (i <s.length() -1 &&map.get(s.charAt(i)) <map.get(s.charAt(i+1)))
26+
res -=map.get(s.charAt(i));
27+
elseres +=map.get(s.charAt(i));
28+
}
29+
returnres;
30+
}
31+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp