Skip to content

Commit 03eb133

Browse files
committed
22w1: add 13 leetcode js solution
1 parent 25e472a commit 03eb133

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@
429429
|16|[3Sum Closest](https://leetcode.com/problems/3sum-closest/)| |Medium|
430430
|15|[3Sum](https://leetcode.com/problems/3sum/)| [js](./algorithms/3Sum/3Sum.js) |Medium|
431431
|14|[Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix/)| [js](./algorithms/longestCommonPrefix/longestCommonPrefix.js) |Easy|
432-
|13|[Roman to Integer](https://leetcode.com/problems/roman-to-integer/)| |Easy|
432+
|13|[Roman to Integer](https://leetcode.com/problems/roman-to-integer/)| [js](./algorithms/romanToInteger/solution.js) |Easy|
433433
|12|[Integer to Roman](https://leetcode.com/problems/integer-to-roman/)| |Medium|
434434
|11|[Container With Most Water](https://leetcode.com/problems/container-with-most-water/)| [js](./algorithms/containerWithMostWater/containerWithMostWater.js) |Medium|
435435
|10|[Regular Expression Matching](https://leetcode.com/problems/regular-expression-matching/)| |Hard|

algorithms/romanToInteger/solution.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
var romanToInt = function (s) {
6+
const map = {
7+
I: 1,
8+
V: 5,
9+
X: 10,
10+
L: 50,
11+
C: 100,
12+
D: 500,
13+
M: 1000,
14+
}
15+
let array = s.split('').map(item => map[item]);
16+
for (let x = 0, y = 1; x < array.length - 1; x++, y++) {
17+
if (array[x] < array[y]) {
18+
array[y] = array[y] - array[x]
19+
array[x] = 0;
20+
}
21+
}
22+
return array.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
23+
};

0 commit comments

Comments
 (0)