Skip to content

Commit aa60d92

Browse files
committed
Valid Parentheses
1 parent 1dfd2bf commit aa60d92

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+
public boolean isValid(String s) {
3+
// μŠ€νƒ... 머리속에 λ– μ˜€λ₯΄λŠ” 생각은 μžˆλŠ”λ° 정리가 μ•ˆλŒ..
4+
// ν•΄μ„€ 읽음
5+
6+
Map<Character, Character> parens = new HashMap<>();
7+
parens.put('(', ')');
8+
parens.put('{', '}');
9+
parens.put('[', ']');
10+
11+
Stack<Character> stack = new Stack<>();
12+
13+
for(char c : s.toCharArray()) {
14+
if (parens.containsKey(c)) {
15+
stack.push(c);
16+
} else {
17+
if (stack.isEmpty() || c != parens.get(stack.pop())) {
18+
return false;
19+
}
20+
}
21+
}
22+
23+
return stack.isEmpty();
24+
}
25+
}

0 commit comments

Comments
Β (0)