문제번호 : 9012
문제
9012
접근
가정
풀어보기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;
import java.util.Stack;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Stack<Character> stack = new Stack<>();
while(true) {
String str = br.readLine()+"\n";
char[] charList = str.toCharArray();
//for문 처음부터 끝까지
for (char c : charList) {
if (c == '(') {
stack.push(c);
}
if (c == ')') {
if(stack.isEmpty()){
stack.push(c);
break;
}
stack.pop();
}
}
if (stack.isEmpty() && str.contains("(")) {
System.out.println("YES");
} else if(str.contains("(")){
System.out.println("NO");
}
stack.clear();
if(br.equals("")){
break;
}
}
}
}
시행착오
- () 괄호가 없을 때 YES 가 나와서 and 연산을 추가함
- 마지막 값은 “\n” 줄바꿈이 없어서 표시가 안 되었음
- 메모리 부족은 해결 못함
참고자료
- for(string : string[]) 순회
- Stack : push() 값 넣기 , pop() 값 빼기 , isEmpty()
문제번호 : 1966
문제
20291
접근
가정
풀어보기
시행착오
참고자료
문제번호 : 1406번
문제
1406번
접근
- String Builder + index 를 이용
가정
- String Builder + index 를 이용
풀어보기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
//
sb.append(br.readLine());
int index = sb.length();
char c1 = 0;
char c2 = 0;
while(true){
String str = br.readLine()+"\n";
c1=str.charAt(0);
if(str.length()>2){
c2=str.charAt(2);
}
switch(c1){
case 'L' : if(index>=1)index-=1;
break;
case 'D' : if(index<sb.length())index+=1;
break;
case 'B' : if(index>0){
sb.deleteCharAt(index-1);
index-=1;
}
break;
case 'P' :
if(index==0){
sb.insert(index,c2);
index+=1;
} else if(index==sb.length()){
sb.insert(index,c2);
index+=1;
} else {
sb.insert(index,c2);
}
break;
}
}
}
}
시행착오
참고자료
- sb.append() : sb 뒤에 붙임
- sb.insert() 인덱스 위치에 값을 추가함
- sb.deleteCharAt() 인덱스 위치를 삭제