Codility #10. Nesting

고독한 키쓰차·2021년 7월 24일
0

코딩테스트

목록 보기
12/16

스택 쓰면 되는 문제.
너무 easy 했었다. 예외 케이스에 대해서만 더 꼼꼼하게 생각하고 빠르게 문제 풀기.
미리 수도코드로 다 짜놓고 코딩하기.

import java.util.*;

class Solution {
    public int solution(String S) {
        int N = S.length();
        if(N == 0) {
            return 1;
        }
        Stack stack = new Stack();
        char temp;
        for(int i = 0; i < N; i++) {
            temp = S.charAt(i);
            if(temp == '(') {
                stack.push(temp);
            }else {
                if(stack.isEmpty()) {
                    return 0;
                }else {
                    stack.pop();
                }
            }
        }
        if(!stack.isEmpty()) {
            return 0;
        }else {
            return 1;
        }

    }
}

profile
Data Scientist or Gourmet

0개의 댓글