Notice
Recent Posts
Recent Comments
Today
Total
01-11 01:45
Link
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Partially Committed

[์Šคํƒ/ํ] ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ (JAVA) ๋ณธ๋ฌธ

๐Ÿ”ฅ Algorithm || ๋ฌธ์ œํ’€์ด/PS

[์Šคํƒ/ํ] ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ (JAVA)

WonderJay 2022. 9. 3. 09:35
728x90
๋ฐ˜์‘ํ˜•
SMALL

https://school.programmers.co.kr/learn/courses/30/lessons/12909

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

import java.io.IOException;
import java.util.Stack;

class Solution {
    boolean solution(String s) throws IOException {
        boolean answer = true;
        Stack<Character> st = new Stack<>();
        char[] arr = s.toCharArray();

        for(int i = 0 ; i < s.length(); i++){
            if(st.isEmpty()){
                if(arr[i] == ')'){
                    return false;
                }
                st.push(arr[i]);
                continue;
            }
            if(arr[i] == '('){
                st.push(arr[i]); continue;
            }
            else{ // arr[i] == ')'
                if(st.peek() == '(') st.pop();
                else return false;
            }

        }

        if(st.isEmpty()) answer = true;
        else answer = false;

        return answer;
    }
}
728x90
๋ฐ˜์‘ํ˜•
LIST
Comments