Skip to content

Commit e770311

Browse files
committed
0020 by HashMap
1 parent cf829de commit e770311

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

0020v2.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
//取自一个评论,侵删
3+
HashMap<Character, Character> paranthesesMap = new HashMap<>();
4+
5+
public Solution(){
6+
this.paranthesesMap.put('(', ')');
7+
this.paranthesesMap.put('[', ']');
8+
this.paranthesesMap.put('{', '}');
9+
}
10+
11+
public boolean isValid(String s) {
12+
Stack<Character> myStack = new Stack<Character>();
13+
for(char c: s.toCharArray()) {
14+
if (paranthesesMap.keySet().contains(c)) myStack.push(paranthesesMap.get(c));
15+
else if (myStack.isEmpty() || c != myStack.pop()) return false;
16+
}
17+
return myStack.isEmpty();
18+
}
19+
}

0 commit comments

Comments
 (0)