File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed
Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * <a href="https://leetcode.com/problems/valid-parentheses/">week06-1.valid-parentheses</a>
3+ * <li>Description: determine if the input string is valid </li>
4+ * <li>Topics: String, Stack </li>
5+ * <li>Time Complexity: O(N), Runtime 2ms </li>
6+ * <li>Space Complexity: O(N), Memory 41.98MB </li>
7+ */
8+
9+ class Solution {
10+ public boolean isValid (String s ) {
11+ Deque <Character > stack = new ArrayDeque <>();
12+ for (char c : s .toCharArray ()) {
13+ if (isOpenBracket (c )){
14+ stack .push (c );
15+ continue ;
16+ }
17+
18+ if (stack .isEmpty () || isNoneMatchBracket (stack .pop (), c )) {
19+ return false ;
20+ }
21+ }
22+
23+ return stack .isEmpty ();
24+ }
25+
26+ private boolean isOpenBracket (char open ) {
27+ return open == '(' || open == '{' || open == '[' ;
28+ }
29+
30+ private boolean isNoneMatchBracket (char open , char close ) {
31+ if (open == '(' ) return close != ')' ;
32+ if (open == '{' ) return close != '}' ;
33+ if (open == '[' ) return close != ']' ;
34+ return true ;
35+ }
36+ }
You can’t perform that action at this time.
0 commit comments