8000 13th problem · Adtrex/LeetcodeJs@5df7d86 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5df7d86

Browse files
committed
13th problem
1 parent e7e4551 commit 5df7d86

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

12. Integer to Roman.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var intToRoman = function(num) {
2+
let ans = ""
3+
// Store all the roman numerals
4+
let string = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"]
5+
// Store all roman numalrs in integers
6+
let value = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
7+
for(let i=0; num != 0; i++){
8+
// while num is greater then value
9+
while(num >= value[i]){
10+
// subtract value from num
11+
num -= value[i]
12+
// add roman string to answer
13+
ans += string[i]
14+
}
15+
}
16+
return ans;
17+
}

13. Roman to Integer.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
var romanToInt = function(s) {
2+
let ans = 0;
3+
let [obj1,obj2] = [{"I":2,"X":20},{"I":2,"X":20,"C":200}]
4+
for(let i = 0 ; i < s.length;i++){
5+
if( s[i] == "I" ){ans += 1}
6+
if( s[i] == "V" ){
7+
ans += 5
8+
if( s[i-1] == "I" ) {ans -= 2}
9+
}
10+
if( s[i] == "X" ){
11+
ans += 10
12+
if( s[i-1] == "I" ) {ans -= 2}
13+
}
14+
if( s[i] == "L" ){
15+
ans += 50
16+
if(obj1[s[i-1]]){ans -= obj1[s[i-1]]}
17+
}
18+
if( s[i] == "C" ){
19+
ans += 100
20+
if(obj1[s[i-1]]){ans -= obj1[s[i-1]]}
21+
}
22+
if( s[i] == "D" ){
23+
ans += 500
24+
if(obj2[s[i-1]]){ans -= obj2[s[i-1]]}
25+
}
26+
if( s[i] == "M"){
27+
ans += 1000
28+
if(obj2[s[i-1]]){ans -= obj2[s[i-1]]}
29+
}
30+
}
31+
return ans
32+
};

0 commit c 306C omments

Comments
 (0)
0