괄호검사를 하는 알고리즘
- stack으로 해결
package test2;
import java.util.Scanner;
import java.util.Stack;
import java.util.Stack;
public class Main {
static int tree[][];
static int t[][];
static int n;
static int check[];
public static void main(String[] args) {
Stack<Character> st = new Stack<Character>();
Scanner sc = new Scanner(System.in);
String temp = sc.next();
for(int i=0;i<temp.length();i++) {
if(temp.charAt(i)=='(')st.push('(');
else {
if(st.isEmpty()) {
System.out.println("false");
return;
}
st.pop();
}
}
if(st.isEmpty())
System.out.println("true");
else System.out.println("false");
}
static int tree[][];
static int t[][];
static int n;
static int check[];
public static void main(String[] args) {
Stack<Character> st = new Stack<Character>();
Scanner sc = new Scanner(System.in);
String temp = sc.next();
for(int i=0;i<temp.length();i++) {
if(temp.charAt(i)=='(')st.push('(');
else {
if(st.isEmpty()) {
System.out.println("false");
return;
}
st.pop();
}
}
if(st.isEmpty())
System.out.println("true");
else System.out.println("false");
}
}