We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent e210b7a commit 3a92bffCopy full SHA for 3a92bff
Stacks/20_Valid_Parentheses.java
@@ -1,27 +1,25 @@
1
class Solution {
2
- public static boolean isValid(String s) {
+ public boolean isValid(String s) {
3
if (s == null || s.length() == 0) {
4
return true;
5
}
6
7
- Stack<Character> st = new Stack<Character>();
+ Stack<Character> st = new Stack<>();
8
9
- for (Character c : s.toCharArray()) {
+ for (char c : s.toCharArray()) {
10
if (c == '(') {
11
st.push(')');
12
} else if (c == '{') {
13
st.push('}');
14
} else if (c == '[') {
15
st.push(']');
16
- } else if (st.empty() || st.pop() != c) {
+ } else if (st.isEmpty() || c != st.peek()) {
17
return false;
18
+ } else {
19
+ st.pop();
20
21
22
- if (!st.empty()) {
- return false;
23
- }
24
-
25
- return true;
+ return st.isEmpty();
26
27
0 commit comments