Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ea2263c

Browse files
String: 20. Valid Parentheses
1 parent f7c0308 commit ea2263c

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

‎String/20. Valid Parentheses.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// https://leetcode.com/problems/valid-parentheses/description/
2+
/**
3+
* 20. Valid Parentheses
4+
* @param {string} s
5+
* @return {boolean}
6+
*/
7+
const map = {
8+
'(': ')',
9+
'{': '}',
10+
'[': ']',
11+
};
12+
var isValid = function(s) {
13+
if (s.length % 2 !== 0) return false;
14+
const stack = [];
15+
for (let i = 0; i < s.length; i++) {
16+
if (map[s[i]]) {
17+
stack.push(s[i]);
18+
} else if (s[i] === map[stack.pop()]) {
19+
continue
20+
} else {
21+
return false;
22+
}
23+
}
24+
if (stack.length > 0) return false;
25+
return true;
26+
};
27+
isValid('()[]{}');
28+
isValid('()');
29+
isValid('([)]');
30+
isValid('{[]}');
31+
isValid('');

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /