Skip to content

Commit b2273cc

Browse files
authored
Update 32-longest-valid-parentheses.js
1 parent 910792a commit b2273cc

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

32-longest-valid-parentheses.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,42 @@ const longestValidParentheses = function(s) {
3636

3737
return longest
3838
}
39+
40+
// another
41+
42+
/**
43+
* @param {string} s
44+
* @return {number}
45+
*/
46+
const longestValidParentheses = function (s) {
47+
let res = 0,
48+
stk = [],
49+
n = s.length,
50+
idxStk = []
51+
for (let i = 0; i < n; i++) {
52+
const ch = s[i]
53+
if (stk.length && stk[stk.length - 1] === '(' && ch === ')')
54+
stk.pop(), idxStk.pop()
55+
else stk.push(ch), idxStk.push(i)
56+
res = Math.max(res, i - (idxStk.length ? idxStk[idxStk.length - 1] : -1))
57+
}
58+
return res
59+
}
60+
/**
61+
* @param {string} s
62+
* @return {number}
63+
*/
64+
const longestValidParentheses = function (s) {
65+
let res = 0,
66+
stk = [],
67+
n = s.length,
68+
idxStk = []
69+
for (let i = 0; i < n; i++) {
70+
const ch = s[i]
71+
if (stk.length && stk[stk.length - 1] === '(' && ch === ')')
72+
stk.pop(), idxStk.pop()
73+
else stk.push(ch), idxStk.push(i)
74+
res = Math.max(res, i - (idxStk.length ? idxStk[idxStk.length - 1] : -1))
75+
}
76+
return res
77+
}

0 commit comments

Comments
 (0)