Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

21.03.02 - [BOJ] 10828. 스택 #20

Closed
suhyunsim opened this issue Mar 7, 2021 · 0 comments
Closed

21.03.02 - [BOJ] 10828. 스택 #20

suhyunsim opened this issue Mar 7, 2021 · 0 comments
Assignees
Labels
성공 맞은 문제 스택 stack 실버 BOJ - 실버

Comments

@suhyunsim
Copy link
Owner

suhyunsim commented Mar 7, 2021

문제

핵심 아이디어

  • 단순 스택 기능 구현

어려운 점, 실수

  • 시간 초과 조심
  • 스택 안 쓰고 하는 방법도 있음

풀이

package main.java.com.poogle.BOJ.Q10828;

import java.io.*;
import java.util.Stack;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        Stack<String> stack = new Stack<>();
        while (n-- > 0) {
            String[] command = br.readLine().split(" ");
            switch (command[0]) {
                case "push":
                    stack.push(command[1]);
                    break;
                case "pop":
                    if (!stack.isEmpty()) {
                        System.out.println(stack.pop());
                    } else {
                        System.out.println("-1");
                    }
                    break;
                case "size":
                    System.out.println(stack.size());
                    break;
                case "empty":
                    if (stack.isEmpty()) {
                        System.out.println("1");
                    } else {
                        System.out.println("0");
                    }
                    break;
                case "top":
                    if (!stack.isEmpty()) {
                        System.out.println(stack.peek());
                    } else {
                        System.out.println("-1");
                    }
                    break;
                default:
                    break;
            }
        }
        br.close();
    }
}
package main.java.com.poogle.BOJ.Q10828;

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

public class Main {
    public static int[] stack;
    public static int size = 0;

    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 = new int[N];
        while (N-- > 0) {
            st = new StringTokenizer(br.readLine(), " ");
            switch (st.nextToken()) {
                case "push":
                    push(Integer.parseInt(st.nextToken()));
                    break;
                case "pop":
                    sb.append(pop()).append('\n');
                    break;
                case "size":
                    sb.append(size()).append('\n');
                    break;
                case "empty":
                    sb.append(empty()).append('\n');
                    break;
                case "top":
                    sb.append(top()).append('\n');
                    break;
            }
        }
        System.out.println(sb);
    }

    public static void push(int item) {
        stack[size] = item;
        size++;
    }

    public static int pop() {
        if (size == 0) {
            return -1;
        } else {
            int res = stack[size - 1];
            stack[size - 1] = 0;
            size--;
            return res;
        }
    }

    public static int size() {
        return size;
    }

    public static int empty() {
        if (size == 0) {
            return 1;
        } else {
            return 0;
        }
    }

    public static int top() {
        if (size == 0) {
            return -1;
        } else {
            return stack[size - 1];
        }
    }
}
@suhyunsim suhyunsim self-assigned this Mar 7, 2021
@suhyunsim suhyunsim added 성공 맞은 문제 스택 stack 실버 BOJ - 실버 labels Mar 8, 2021
@suhyunsim suhyunsim added this to the 3월 1주 차 milestone Mar 8, 2021
suhyunsim added a commit that referenced this issue Mar 8, 2021
스택으로 풀이

Issue #20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
성공 맞은 문제 스택 stack 실버 BOJ - 실버
Projects
None yet
Development

No branches or pull requests

1 participant