Skip to content

Commit a602cf6

Browse files
committed
Update 0013-roman-to-integer.js
1 parent 9218619 commit a602cf6

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

javascript/0013-roman-to-integer.js

+28
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,31 @@ var romanToInt = function (s) {
6363

6464
// Runtime: 148 ms, faster than 80.16% of JavaScript online submissions for Roman to Integer.
6565
// 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/

0 commit comments

Comments
 (0)