728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ๐Ÿ˜ต‍๐Ÿ’ซ

https://school.programmers.co.kr/learn/courses/30/lessons/12909?language=java

 

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

SW๊ฐœ๋ฐœ์ž๋ฅผ ์œ„ํ•œ ํ‰๊ฐ€, ๊ต์œก, ์ฑ„์šฉ๊นŒ์ง€ Total Solution์„ ์ œ๊ณตํ•˜๋Š” ๊ฐœ๋ฐœ์ž ์„ฑ์žฅ์„ ์œ„ํ•œ ๋ฒ ์ด์Šค์บ ํ”„

programmers.co.kr

 

๋‚ด ํ’€๐Ÿฆท

import java.util.*;

class Solution {
    boolean solution(String s) {
        Stack<String> str = new Stack<>();
        boolean answer = true;
        
        for(int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            
            if(c == '(') {
                str.push(String.valueOf(c));
            } else if(c ==')') {
                if(str.isEmpty()) {
                    answer = false;
                    break;
                }
                str.pop();
            }
        }
        
        if (!str.isEmpty()) {
            answer = false;
        }
        return answer;
    }
}

 

์ •๋ง์ •๋ง ์˜ค๋žœ๋งŒ์— ๊ด„ํ˜ธ ๊ฒ€์‚ฌ ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ๋‹ค ใ…Žใ…Žใ…Ž

์ง„์งœ ๊ด„ํ˜ธ ๊ฒ€์‚ฌ ์ถ”์–ต์ด ๋งŽ์€ ๋ฌธ์ œ๋ผ ๋„ˆ๋ฌด ๊ธฐ์–ต๋‚˜๋Š”๋ฐ ํŒŒ์ด์ฌ ํ–ˆ๋˜์‹œ์ ˆ์ด๋ผ ์ž ์‹œ ํ ์นซํ–ˆ๋‹ค ใ…Žใ…Žใ…Žใ…Ž

๋‹น์—ฐํžˆ ์Šคํƒ ์จ์„œ ํ’€์–ด์•ผ ๊ฒ ๋‹ค ํ–ˆ๋Š”๋ฐ ๋‹ค๋ฅธ ์‚ฌ๋žŒ ํ’€์ด๊ฐ€ ์Šคํƒ๋„ ์“ฐ์ง€ ์•Š๊ณ  ์ •๋ง ๊ฐ„๋‹จํ•˜๊ฒŒ ์ผ๋˜ ํ’€์ด๊ฐ€ ์žˆ์–ด์„œ ํ•œ๋ฒˆ ๊ฐ€์ ธ์™€ ๋ดค๋‹ค...

 

// ๋‹ค๋ฅธ ์‚ฌ๋žŒ ํ’€์ด

class Solution {
    boolean solution(String s) {
        boolean answer = false;
        int count = 0;
        for(int i = 0; i<s.length();i++){
            if(s.charAt(i) == '('){
                count++;
            }
            if(s.charAt(i) == ')'){
                count--;
            }
            if(count < 0){
                break;
            }
        }
        if(count == 0){
            answer = true;
        }

        return answer;
    }
}

 

์ฐธ๊ณ ํ•˜๋ฉด ์ข‹์„ ๊ฐœ๋…๋“ค โœ๏ธ

์ŠคํŠธ๋ง์œผ๋กœ ๋ณ€ํ™˜ : String.valueOf(๋ฐ”๊ฟ€ ๊ฐ’);

728x90
๋ฐ˜์‘ํ˜•

+ Recent posts