Skip to content

Commit 89c72b4

Browse files
author
luojing
committed
add 0013 solution
1 parent 55971ef commit 89c72b4

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/com/blankj/easy/_0009/LJSolution.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
package com.blankj.easy._009;
2-
32
/**
43
* @author luojing
54
* @description LJSolution
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.blankj.easy._013;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* @author luojing
8+
* @description LJSolution
9+
* @date 2022/07/17
10+
*/
11+
12+
public class LJSolution {
13+
public int romanToInt(String s) {
14+
Map<Character, Integer> map = new HashMap<>();
15+
map.put('I', 1);
16+
map.put('V', 5);
17+
map.put('X', 10);
18+
map.put('L', 50);
19+
map.put('C', 100);
20+
map.put('D', 500);
21+
map.put('M', 1000);
22+
int len = s.length();
23+
int sum = map.get(s.charAt(len -1));
24+
for (int i = len - 2; i >= 0 ; --i) {
25+
if (map.get(s.charAt(i)) < map.get(s.charAt(i + 1))) {
26+
sum -= map.get(s.charAt(i));
27+
} else {
28+
sum += map.get(s.charAt(i));
29+
}
30+
}
31+
return sum;
32+
}
33+
34+
public static void main(String[] args) {
35+
LJSolution ljSolution = new LJSolution();
36+
System.out.println(ljSolution.romanToInt("DCXXI")); // 621
37+
System.out.println(ljSolution.romanToInt("CCCXLVIII")); // 348
38+
}
39+
40+
}

0 commit comments

Comments
 (0)