File tree 1 file changed +28
-0
lines changed
1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change @@ -63,3 +63,31 @@ var romanToInt = function (s) {
63
63
64
64
// Runtime: 148 ms, faster than 80.16% of JavaScript online submissions for Roman to Integer.
65
65
// Memory Usage: 47.5 MB, less than 18.15% of JavaScript online submissions for Roman to Integer.
66
+
67
+ function romanToInt ( s ) {
68
+ let sum = 0
69
+ let next = null
70
+ const romanArr = {
71
+ "I" : 1 ,
72
+ "V" : 5 ,
73
+ "X" : 10 ,
74
+ "L" : 50 ,
75
+ "C" : 100 ,
76
+ "D" : 500 ,
77
+ "M" : 1000
78
+ }
79
+ for ( let i = 0 ; i < s . length ; i ++ ) {
80
+ next = s [ i + 1 ] || null
81
+ const curr = s [ i ]
82
+ if ( romanArr [ next ] > romanArr [ curr ] ) {
83
+ sum -= romanArr [ curr ]
84
+ continue
85
+ }
86
+ sum += romanArr [ curr ]
87
+ }
88
+ return sum
89
+ } ;
90
+
91
+ // Runtime 97 ms
92
+ // Memory usage: 47.8 MB
93
+ // https://leetcode.com/problems/roman-to-integer/submissions/1020204566/
You can’t perform that action at this time.
0 commit comments