[๋ฌธ์ ๋งํฌ]
https://www.acmicpc.net/problem/9012
[ํ์ด์์ค]
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
32
33
34
35
36
37
38
39
40
|
package algorithm;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
public class Stack9012 {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int testcase=Integer.parseInt(br.readLine());
StringBuilder sb=new StringBuilder();
while(testcase-->0) {
String str=br.readLine();
boolean isVps=true;
Stack<Character> marks=new Stack<Character>();
for(int i=0;i<str.length();i++) {
char temp=str.charAt(i);
if(temp=='(') {
marks.push('(');
} else {
if(marks.isEmpty()) {
isVps=false;
break;
}
}
}
if (isVps && marks.isEmpty()) {
sb.append("YES \n");
} else {
sb.append("NO \n");
}
}
System.out.println(sb);
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
[๋๋์ ]
- ๋ณ์์ ์ธํ๋ ์์น ์ ๋ฐ ์ ๋ฐ ์ฃผ์!
- ๊ดํธ๋ฌธ์ ๋ Stack!์ด์ฉ, ๋๋ ์ฌ๋ ๊ดํธ๋ฅผ ๋ฃ์๋๋ฐ ๋ซ๋๊ดํธ๋ฃ๋๊ฒ ์ฌ๋ฌ๊ดํธ์๋๊ฒฝ์ฐ์๋ ํธํ๋ค๊ณ ํ๋๋ผ?
- ์ฒ์์ isVps๋ฅผ String์ผ๋ก ํ์ฉํ๋๋ฐ ๊ทธ๊ฑด ์์๋๋๊ฑธ๊น? ์ฐ์ booleanํํ๋ก ํ๋๊น OK!
์ด์ ๊ฐ ๋ญ์ง..
'Algoritm > Quiz-Solutions' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Leetcode 1] two Sum (0) | 2020.08.02 |
---|---|
[๋ฐฑ์ค 2156] ํฌ๋์ฃผ ์์ (Java) - DP (0) | 2020.05.07 |
[๋ฐฑ์ค 1260] DFS์ BFS (Java) - DFS,BFS (0) | 2020.05.07 |
[๋ฐฑ์ค 10854] ํ (JAVA) - Queue (0) | 2020.05.03 |
[๋ฐฑ์ค 15953] ์๊ธํํฐ (JAVA) (0) | 2020.05.02 |
๋๊ธ