File tree Expand file tree Collapse file tree 2 files changed +40
-1
lines changed Expand file tree Collapse file tree 2 files changed +40
-1
lines changed Original file line number Diff line number Diff line change 1
1
package com .blankj .easy ._009 ;
2
-
3
2
/**
4
3
* @author luojing
5
4
* @description LJSolution
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments