Skip to content

Commit fa01469

Browse files
author
piggy1991
committed
del
1 parent 6e8527d commit fa01469

File tree

2 files changed

+31
-41
lines changed

2 files changed

+31
-41
lines changed

stack/IsValid.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,37 @@
33
import java.util.Stack;
44

55
public class IsValid {
6+
public boolean isValid1(String s) {
7+
if (s == null || s.length() == 0) {
8+
return true;
9+
}
10+
11+
Stack<Character> stack = new Stack<Character>();
12+
int len = s.length();
13+
for (int i = 0; i < len; i++) {
14+
char c = s.charAt(i);
15+
if (stack.isEmpty()) {
16+
if (c == ']' || c == ')' || c == '}') {
17+
return false;
18+
}
19+
stack.push(c);
20+
continue;
21+
}
22+
23+
if (c == ')' && stack.peek() == '('
24+
|| c == ']' && stack.peek() == '['
25+
|| c == '}' && stack.peek() == '{'
26+
) {
27+
stack.pop();
28+
} else if (c == '(' || c == '[' || c == '{') {
29+
stack.push(c);
30+
} else {
31+
return false;
32+
}
33+
}
34+
35+
return stack.isEmpty();
36+
}
637
public boolean isValid(String s) {
738
if (s == null) {
839
return false;

string/IsValid.java

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)