Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1fc6583

Browse files
Add Solution.java to problems 0020
1 parent 2e8778f commit 1fc6583

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
Map<String,String> map;
3+
4+
public Solution() {
5+
map = new HashMap();
6+
map.put("(", ")");
7+
map.put(")", "(");
8+
map.put("{", "}");
9+
map.put("}", "{");
10+
map.put("[", "]");
11+
map.put("]", "[");
12+
}
13+
14+
public boolean isValid(String s) {
15+
Stack<String> stack = new Stack();
16+
for (int i = 0; i < s.length(); i ++) {
17+
if (!stack.isEmpty() && map.get(String.valueOf(s.charAt(i))).equals(stack.peek())) {
18+
stack.pop();
19+
} else {
20+
stack.push(String.valueOf(s.charAt(i)));
21+
}
22+
}
23+
return stack.isEmpty();
24+
}
25+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /