stack

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Solution {
/**
* stack
*/
public boolean isValid(String s) {
if (s.length() % 2 == 1) return false;

Deque<Character> st = new ArrayDeque<>();
char[] charArray = s.toCharArray();
for (char ch : charArray) {
switch (ch) {
case '(':
case '[':
case '{':
st.push(ch);
break;

case ')':
if (st.isEmpty() || st.pop() != '(')
return false;
break;
case ']':
if (st.isEmpty() || st.pop() != '[')
return false;
break;
case '}':
if (st.isEmpty() || st.pop() != '{')
return false;
break;
}
}
return st.isEmpty();
}
}

Comments
Recent Posts
Untitled
Categories
Tags
Website Info
Article Count :
2
Total Word Count :
1.6k
Unique Visitors :
Page Views :
Last Update :