본문 바로가기

stack5

쇠막대기 1. 레이저는 여는 괄호와 닫는 괄호의 인접한 쌍 ‘( ) ’ 으로 표현된다. 또한, 모든 ‘( ) ’는 반드시 레이저를 표현한다. 2. 쇠막대기의 왼쪽 끝은 여는 괄호 ‘ ( ’ 로, 오른쪽 끝은 닫힌 괄호 ‘) ’ 로 표현된다. '('를 stack에 쌓는다. ')'일 때 stack.pop() → 인접한 쌍 '()'이면 answer += stack.size() 인접하지 않으면 answer++ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public static int solution(String str) { int answer = 0; Stack stack = new Stack(); for (int i = 0; i 2023. 8. 29.
postfix 53+ → 5 + 3 5가 lt, 3이 rt str을 char배열로 변환하기 때문에 숫자 '0'이 들어온다면 아스키 코드 48로 변환된다. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public static int solution(String str) { int answer = 0; Stack stack = new Stack(); for (char c : str.toCharArray()) { if(Character.isDigit(c)) { stack.push(c - 48); } else { int rt = stack.pop(); int lt = stack.pop(); if(c == '+') {stack.push(lt + rt);} if(c == '-') {stack... 2023. 8. 29.
인형뽑기(stack) 인형의 위치는 for(i : b){ for(int j = 0; j < a.length ;j++){ a[j][i - 1] 인형을 꺼내고 그 자리를 0으로 만든다. 인형 2개를 터뜨리기 때문에 answer += 2이다. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 public static int solution(int n, int[][] a, int m, int[] b) { int answer = 0; Stack stack = new Stack(); for (int i : b) { for (int j = 0; j 2023. 8. 29.
Stack 1 2 3 4 5 6 7 8 9 10 11 12 13 14 public static void solution(String str) { Stack stack = new Stack(); for (char c : str.toCharArray()) { if(c != ')') { stack.push(c); } else {// c == ')' while(stack.pop() != '(');// 계속 pop되다가 '('를 만나면 pop되고 while문을 빠져나옴 } } for (char c : stack) { System.out.print(c); } } Colored by Color Scripter cs 출처 : 인프런 자바(Java) 알고리즘 문제풀이 입문: 코딩테스트 대비 2023. 8. 28.