forked from JadeZYX/Java_LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP0013RomanToInt.java
More file actions
47 lines (46 loc) · 1.32 KB
/
Copy pathP0013RomanToInt.java
File metadata and controls
47 lines (46 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.util.HashMap;
public class P0013RomanToInt {
public int romanToInt(String s) {
HashMap<Character,Integer>map=new HashMap<>();
map.put('I',1);
map.put('V',5);
map.put('X',10);
map.put('L',50);
map.put('C',100);
map.put('D',500);
map.put('M',1000);
int num=0;
for(int i=0;i<s.length();i++){
if(i+1<s.length()&&s.charAt(i+1)>s.charAt(i)){
num-=map.get(s.charAt(i));
}
else{
num+=map.get(s.charAt(i));
}
}
return num;
}
public int romanToInt1(String s) {
HashMap<Character,Integer>map=new HashMap<>();//O(1)的space
map.put('I',1);
map.put('V',5);
map.put('X',10);
map.put('L',50);
map.put('C',100);
map.put('D',500);
map.put('M',1000);
int num=0;
for(int i=0;i<s.length();i++){
if(i+1<s.length()&&map.get(s.charAt(i))<map.get(s.charAt(i+1))){
num+=(map.get(s.charAt(i+1))-map.get(s.charAt(i)));
i+=1;
}
else{
num+=map.get(s.charAt(i));
}
}
return num;
}
}
//P0013RomanToInt p13=new P0013RomanToInt();
//System.out.println(p13.romanToInt("MCMXCIV"));