Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd9d562f

Browse files
authored
Merge pull request#83 from berkslv/main
Added javascript solution for 13. Roman to Integer
2 parents9acf325 +9638abd commitd9d562f

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

‎javascript/13-Roman-to-Integer.js‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
*@param {string} s
3+
*@return {number}
4+
*/
5+
varromanToInt=function(s){
6+
letromans={
7+
I:1,
8+
V:5,
9+
X:10,
10+
L:50,
11+
C:100,
12+
D:500,
13+
M:1000,
14+
};
15+
16+
letarr=s.split("");
17+
18+
letsum=0;
19+
20+
for(leti=arr.length-1;i>=0;i--){
21+
// IV : 4
22+
if(romans[arr[i]]===romans["V"]){
23+
if(romans[arr[i-1]]===romans["I"]){
24+
sum-=1*2;
25+
}
26+
}
27+
// IX : 4
28+
if(romans[arr[i]]===romans["X"]){
29+
if(romans[arr[i-1]]===romans["I"]){
30+
sum-=1*2;
31+
}
32+
}
33+
// XL : 40
34+
if(romans[arr[i]]===romans["L"]){
35+
if(romans[arr[i-1]]===romans["X"]){
36+
sum-=10*2;
37+
}
38+
}
39+
// XC : 90
40+
if(romans[arr[i]]===romans["C"]){
41+
if(romans[arr[i-1]]===romans["X"]){
42+
sum-=10*2;
43+
}
44+
}
45+
// CD : 400
46+
if(romans[arr[i]]===romans["D"]){
47+
if(romans[arr[i-1]]===romans["C"]){
48+
sum-=100*2;
49+
}
50+
}
51+
// CM : 900
52+
if(romans[arr[i]]===romans["M"]){
53+
if(romans[arr[i-1]]===romans["C"]){
54+
sum-=100*2;
55+
}
56+
}
57+
58+
sum+=romans[arr[i]];
59+
}
60+
61+
returnsum;
62+
};
63+
64+
// Runtime: 148 ms, faster than 80.16% of JavaScript online submissions for Roman to Integer.
65+
// Memory Usage: 47.5 MB, less than 18.15% of JavaScript online submissions for Roman to Integer.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp