Skip to content

Commit 3a92bff

Browse files
committed
Update 20_Valid_Parentheses.java
1 parent e210b7a commit 3a92bff

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

Stacks/20_Valid_Parentheses.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
11
class Solution {
2-
public static boolean isValid(String s) {
2+
public boolean isValid(String s) {
33
if (s == null || s.length() == 0) {
44
return true;
55
}
66

7-
Stack<Character> st = new Stack<Character>();
7+
Stack<Character> st = new Stack<>();
88

9-
for (Character c : s.toCharArray()) {
9+
for (char c : s.toCharArray()) {
1010
if (c == '(') {
1111
st.push(')');
1212
} else if (c == '{') {
1313
st.push('}');
1414
} else if (c == '[') {
1515
st.push(']');
16-
} else if (st.empty() || st.pop() != c) {
16+
} else if (st.isEmpty() || c != st.peek()) {
1717
return false;
18+
} else {
19+
st.pop();
1820
}
1921
}
2022

21-
if (!st.empty()) {
22-
return false;
23-
}
24-
25-
return true;
23+
return st.isEmpty();
2624
}
2725
}

0 commit comments

Comments
 (0)