Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- bit manipulation
- array
- string
- Binary Tree
- geometry
- 구현
- hash table
- 파이썬
- implement
- Method
- 코테
- sorting
- Math
- Matrix
- dynamic programming
- Number Theory
- Tree
- Counting
- Stack
- 자바
- Data Structure
- Class
- database
- 코딩테스트
- SQL
- Binary Search
- greedy
- simulation
- two pointers
- java
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 20. Valid Parentheses 본문
1. Input
1) 문자열 s
2. Output
1) s가 올바른 괄호 사용을 한 문자열이면 true, 아니면 false를 반환
3. Constraint
1) 1 <= s.length <= 10^4
2) s는 '()[]{}'의 6개의 문자로만 이루어져 있다.
4. Example
Input: s = "()[]{}" -> Output: true
Input: s = "([)]{}" -> Output: false
5. Code
1) 첫 코드(2023/01/05)
import java.util.*;
boolean answer = true;
Stack<Integer> st = new Stack<Integer>();
try{
for(int i=0 ; i<s.length() ; i++){
switch(String.valueOf(s.charAt(i))){
case "(": st.push(0); break;
case "{": st.push(1); break;
case "[": st.push(2); break;
case ")":
if(st.peek()==0) { st.pop(); break; }
else { answer = false; break;}
case "}":
if(st.peek()==1) { st.pop(); break; }
else { answer = false; break;}
default: // "]"
if(st.peek()==2) { st.pop(); break; }
else { answer = false; break;}
} // switch
} // for
}catch(Exception e){ answer=false; }
return answer && st.empty();
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[LeetCode/Easy] 1827. Minimum Operations to Make the Array Increasing (0) | 2023.01.05 |
---|---|
[LeetCode/Easy] 1822. Sign of the Product of an Array (0) | 2023.01.05 |
[LeetCode/Easy] 1816. Truncate Sentence (0) | 2023.01.04 |
[LeetCode/Easy] 1812. Determine Color of a Chessboard Square (0) | 2023.01.04 |
[LeetCode/Easy] 1791. Find Center of Star Graph (0) | 2023.01.04 |