-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtwo-sum.js
29 lines (26 loc) · 890 Bytes
/
two-sum.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.*/
/**
* @param {string} s
* @return {boolean}
*/
var isValid = function(s) {
const stack = []
let isValid = true
for(let i =0; i < s.length; i++){
if(s[i] === "(" || s[i] === "{" || s[i] === "["){
stack.push(s[i])
}else if(stack[stack.length - 1] + s[i] === "()" || stack[stack.length - 1] + s[i] === "{}" || stack[stack.length - 1] + s[i] === "[]"){
stack.pop()
}else{
isValid = false
break;
}
}
if(isValid && stack.length === 0){
return true
}else{
return false
}
};