๊ด€๋ฆฌ ๋ฉ”๋‰ด

Tech Log ๐Ÿ› ๏ธ

๋ฐฑ์ค€ 9012) ๊ด„ํ˜ธ ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ๋‘๊ฐ€์ง€ ๋ฐฉ๋ฒ• ๋ณธ๋ฌธ

์•Œ๊ณ ๋ฆฌ์ฆ˜

๋ฐฑ์ค€ 9012) ๊ด„ํ˜ธ ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ๋‘๊ฐ€์ง€ ๋ฐฉ๋ฒ•

sehaan 2023. 1. 15. 19:41

 

๋ถ„์„

-  VPS๊ฐ€ ๋˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๋Š” '(' ๊ฐ€ ๋” ๋งŽ๊ฑฐ๋‚˜ ')' ๊ฐ€ ๋” ๋งŽ์€ ๊ฒฝ์šฐ๋กœ ์ด ๋‘๊ฐ€์ง€ ์ด๋‹ค.

 

1)

์ฒ˜์Œ์—๋Š” ๋‹จ์ˆœํžˆ ๋‘๊ฐœ์˜ for ๋ฌธ์„ ์ด์šฉํ•˜์—ฌ '('๊ฐ€ ๋‚˜์˜ค๋ฉด push ')'๊ฐ€ ๋‚˜์˜ค๋ฉด pop ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํ•˜์˜€๋‹ค.

ํ•˜์ง€๋งŒ ์ด๋ ‡๊ฒŒ ํ•˜๋‹ค๋ณด๋‹ˆ for๋ฌธ์ด ๋๋‚œ ํ›„์— VPS ํŒ๋‹จ ์—ฌ๋ถ€ ์ฝ”๋“œ(for๋ฌธ์ด ์˜จ์ „ํžˆ ๋๋‚ฌ๋Š”์ง€ , ์Šคํƒ์ด ๋น„์–ด์žˆ๋Š”์ง€ )๊ฐ€ ์ง€์ €๋ถ„ํ•˜์—ฌ์„œ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ๋„ ํ’€์–ด๋ณด์•˜๋‹ค.

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.Buffer;
import java.util.ArrayList;
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));
        StringBuilder sb = new StringBuilder();

        int num = Integer.parseInt(br.readLine());
        for (int i = 0; i < num; i++) {
            Stack s = new Stack<>();
            String str = br.readLine();
 
            for (int j = 0; j < str.length(); j++) {
                if (str.charAt(j) == '(') {

                    s.push(str.charAt(j));
                } else {
                    if (s.size() == 0) {
                        sb.append("NO").append("\n");
                        break;
                    }

                    s.pop();
                }
                if(j==str.length()-1 & s.size() ==0){
                    sb.append("YES").append("\n");
                }
                if(j==str.length()-1 & s.size() !=0){
                    sb.append("NO").append("\n");
                }
            }

        }
        System.out.println(sb);

    }
}

 

2)

๋‘๋ฒˆ์งธ๋กœ๋Š” VPS ํŒ๋‹จ ํด๋ž˜์Šค๋ฅผ ๋”ฐ๋กœ ๋งŒ๋“ค์–ด์„œ YES or NO ๊ฐ’์„ ๋ฆฌํ„ดํ•ด์„œ ์ถœ๋ ฅํ•ด์ฃผ๋Š” ๋ฐฉ์‹์œผ๋กœ ํ•˜์˜€๋‹ค.

์ด๋ ‡๊ฒŒํ•˜๋‹ˆ ์ฝ”๋“œ๊ฐ€ ํ›จ์”ฌ ๊ฐ„๊ฒฐํ•ด์กŒ๋‹ค.

์›๋ž˜๋Š” Stringbuilder์— append๋ฅผ ํ•˜๋Š” ๊ตฌ์กฐ์—ฌ์„œ break๋ฌธ์„ ์‚ฌ์šฉํ•˜๋‹ค๋ณด๋‹ˆ for๋ฌธ์ด ๋‹ค ๋๋‚ฌ๋Š”์ง€์— ๋Œ€ํ•œ ํ™•์ธ๋„ ํ•„์š”ํ–ˆ๋Š”๋ฐ 

return๋ฌธ์„ ์ด์šฉํ•˜๋‹ˆ ๋‹จ์ˆœํžˆ VPS ๊ฐ€ ์•„๋‹ˆ๋ฉด ๋ฐ”๋กœ ์ถœ๋ ฅ์ด ๋˜๋Š” ๊ตฌ์กฐ์—ฌ์„œ ์ง๊ด€์ ์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์งค ์ˆ˜ ์žˆ์—ˆ๋‹ค.

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

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        Stack s = new Stack();
        int num = Integer.parseInt(br.readLine());
        for(int i=0; i < num ; i++){
            s.clear();
            String str = br.readLine();
            System.out.println(checkVPS(str,s));
        }
    }

    public static String checkVPS(String str,Stack s){
        for(int j =0 ; j< str.length() ; j++){
            if(str.charAt(j) == '(') s.push(str.charAt(j));
            if(s.size() == 0) return "NO";
            if(str.charAt(j) == ')') s.pop();
        }
        if(s.size() == 0) return "YES";
        return "NO";
    }

}