백준/자료구조

[백준] 스택 - 10828

믕비 2023. 4. 5. 14:45
반응형

switch-case문으로 풀었음.

직접 stack 구현해서 풀어보기도 해보기**

 

내 코드

메모리 : 18984 KB

시간 : 200 ms

코드길이 : 1302 B

 

1등 코드

메모리 : 12144 KB

시간 : 84 ms

코드길이 : 2912 B

 

[내 코드]

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

public class Main {

	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		StringTokenizer st;
		
		int N = Integer.parseInt(br.readLine());
		
		Stack<Integer> stack = new Stack<Integer>();
		
		for(int n = 0; n < N; n++) {
			st = new StringTokenizer(br.readLine());
			String order = st.nextToken();
			int pushNum;
			int printNum;
			
			switch(order) {
			case "push":
				pushNum = Integer.parseInt(st.nextToken());
				stack.push(pushNum);
				break;
			case "pop":
				if(!stack.isEmpty())
					printNum = stack.pop();
				else
					printNum = -1;
				sb.append(printNum).append('\n');
				break;
			case "size":
				printNum = stack.size();
				sb.append(printNum).append('\n');
				break;
			case "empty":
				if(stack.isEmpty())
					printNum = 1;
				else
					printNum = 0;
				sb.append(printNum).append('\n');
				break;
			case "top":
				if(stack.isEmpty())
					printNum = -1;
				else
					printNum = stack.peek();
				sb.append(printNum).append('\n');
				break;
			}
		}
		System.out.print(sb);

	}

}

[상위권 코드]

import java.io.IOException;

class Main{
    public static void main(String[] args) throws IOException{
        StringBuilder sb = new StringBuilder();
        int N = read_int();
        int[] stack = new int[N+1];
        int top = 0;
        
        while(N-- >0){
            switch(read_char()){
                case 'p'+'u'+'s'+'h':
                    stack[++top] = read_int(); 
                    break;
                case 'p'+'o'+'p':
                    if(top==0) sb.append(-1).append('\n');
                    else sb.append(stack[top--]).append('\n');
                    break;
                case 's'+'i'+'z'+'e':
                    sb.append(top).append('\n');
                    break;
                case 'e'+'m'+'p'+'t'+'y':
                    if(top==0) sb.append(1).append('\n');
                    else sb.append(0).append('\n');
                    break;
                case 't'+'o'+'p':
                    if(top==0) sb.append(-1).append('\n');
                    else sb.append(stack[top]).append('\n');
            }
        }
        System.out.print(sb);
    }
    
    static int read_int() throws IOException{
        int N=0, c;
        while((c=System.in.read())>47){N = (N<<3)+(N<<1)+(c&15);}
        if(c==13) System.in.read();
        return N;
    }
    
    static int read_char() throws IOException{
        int N=0, c;
        while((c=System.in.read())>95){N += c;}
        if(c==13) System.in.read();
        return N;
    }
}
반응형

'백준 > 자료구조' 카테고리의 다른 글

[스택] 17413 : 단어 뒤집기 2  (0) 2023.04.11
[스택] 17298 : 오큰수  (0) 2023.04.11
[백준] 스택 : 9093  (0) 2023.04.06
[백준] 큐 - 10845  (0) 2023.04.06
[백준] 스택 - 1406  (0) 2023.04.05