Created
March 27, 2019 15:22
-
-
Save alsotang/e3ec1f8551daa1ff211f50ea325a0e66 to your computer and use it in GitHub Desktop.
https://leetcode.com/problems/valid-parentheses/ solve by add numbers
This file contains hidden or 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
const map = { | |
"(": 3n, | |
"[": 5n, | |
"{": 7n, | |
")": -3n, | |
"]": -5n, | |
"}": -7n | |
}; | |
var isValid = function(s) { | |
let sum = 0n; | |
for (var i = 0; i < s.length; i++) { | |
const char = s[i]; | |
const v = map[char]; | |
if (v > 0) { | |
sum = (sum + v) * v; | |
} else { | |
const newSum = sum / -v + v; | |
if ((newSum - v) * -v !== sum) { | |
return false; | |
} | |
sum = newSum | |
} | |
} | |
return sum == 0; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment