Created
June 7, 2021 07:04
-
-
Save samandar-boymurodov/4b2b25669c6597267e2a6e14ffd5717b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {string} s | |
* @return {number} | |
*/ | |
var romanToInt = function(s) { | |
const symbols = { | |
"I": 1, | |
"V": 5, | |
"X": 10, | |
"L": 50, | |
"C": 100, | |
"D": 500, | |
"M": 1000 | |
} | |
let acc = 0 | |
while (s.length>0) { | |
if (symbols[s[1]] > symbols[s[0]]) { | |
acc += symbols[s[1]] - symbols[s[0]] | |
s = s.substr(2,) | |
} else { | |
acc += symbols[s[0]] | |
s = s.substr(1,) | |
} | |
} | |
return acc | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
3+4 is equal to 3-1+5. I just didn't think about it