Skip to content

Instantly share code, notes, and snippets.

@samandar-boymurodov
Created June 7, 2021 07:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samandar-boymurodov/4b2b25669c6597267e2a6e14ffd5717b to your computer and use it in GitHub Desktop.
Save samandar-boymurodov/4b2b25669c6597267e2a6e14ffd5717b to your computer and use it in GitHub Desktop.
/**
* @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
};
@samandar-boymurodov
Copy link
Author

3+4 is equal to 3-1+5. I just didn't think about it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment