Java 코딩테스트 공부/Java 백준 문제풀이

자바 백준 10828, 10845, 10866번 문제 - 스택, 큐, 덱

daramG 2022. 10. 21. 17:02

문제 출처 : https://www.acmicpc.net/problem/10828

 

10828번: 스택

첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지

www.acmicpc.net

문제 출처2 : https://www.acmicpc.net/problem/10845

 

10845번: 큐

첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지

www.acmicpc.net

문제 출처3 : https://www.acmicpc.net/problem/10866

 

10866번: 덱

첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지

www.acmicpc.net

 

 

소스코드 )

import java.util.*;
import java.io.*;
public class Main
{
    Stack<Integer> stack = new Stack<>();
    public void solution(String str) {
        // push X
        if (str.charAt(0) == 'p' && str.charAt(1) == 'u') {
			String newStr[] = str.split(" ");
            stack.push(Integer.parseInt(newStr[1]));
        }
        // pop
        else if (str.equals("pop")) {
            if (stack.empty()) System.out.println(-1); 
            else System.out.println(stack.pop());
        }
        // size
        else if (str.equals("size")) {
            System.out.println(stack.size());
        }
        // empty
        else if (str.equals("empty")) {
            if (stack.empty()) System.out.println(1);
            else System.out.println(0);
        }
        // top
        else if (str.equals("top")) {
            if (stack.empty()) System.out.println(-1);
            else System.out.println(stack.peek());
        }
        return;
	}
	public static void main(String[] args) throws IOException {
	    Main T = new Main();
	    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	    int n = Integer.parseInt(br.readLine());
	    for(int i=0; i<n; i++) {
	        String str = br.readLine();
	        T.solution(str);
	    }
		br.close();
	}
}

 

 

큐 버전

import java.util.*;
import java.io.*;
class Main {
	Queue<Integer> queue = new LinkedList<>();
	int backNum = 0;
	public void solution(String str) {
		// push X
		if (str.charAt(0) == 'p' && str.charAt(1) == 'u') {
			String newStr[] = str.split(" ");
			backNum = Integer.parseInt(newStr[1]);
			queue.offer(Integer.parseInt(newStr[1]));
		}
		// pop
		else if (str.equals("pop")) {
			if (queue.isEmpty()) System.out.println("-1");
			else System.out.println(queue.poll());
		}
		// size
		else if (str.equals("size")) {
			System.out.println(queue.size());
		}
		// empty
		else if (str.equals("empty")) {
			if (queue.isEmpty()) System.out.println("1");
			else System.out.println("0");
		}
		// front
		else if (str.equals("front")) {
			if (queue.isEmpty()) System.out.println("-1");
			else System.out.println(queue.peek());
		}
		// back
		else if (str.equals("back")) {
			if (queue.isEmpty()) System.out.println("-1");
			else System.out.println(backNum);
		}
		
	}
	public static void main(String[] args) throws IOException {
		Main T = new Main();
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		while (n-->0) {
			String str = br.readLine();
			T.solution(str);
		}
		br.close();
	}
}

 

덱 버전

import java.util.*;
import java.io.*;
class Main {
	Deque<Integer> dq = new ArrayDeque<Integer>();
	public void solution(String str) {
		if (str.charAt(0) == 'p' && str.charAt(1) == 'u') {
			String newStr[] = str.split(" ");
			// push_front X
			if (str.charAt(5) == 'f') {
				dq.offerFirst(Integer.parseInt(newStr[1]));
			}
			// push_back X
			else if (str.charAt(5) == 'b') {
				dq.offerLast(Integer.parseInt(newStr[1]));
			}
		}
		else if (str.charAt(0) == 'p' && str.charAt(1) == 'o') {
			// pop_front
			if (str.charAt(4) == 'f') {
				if (dq.isEmpty()) System.out.println("-1");
				else System.out.println(dq.pollFirst());
			}
			// pop_back
			else if (str.charAt(4) == 'b') {
				if (dq.isEmpty()) System.out.println("-1");
				else System.out.println(dq.pollLast());
			}
		}
		// size
		else if (str.equals("size")) {
			System.out.println(dq.size());
		}
		// empty
		else if (str.equals("empty")) {
			if (dq.isEmpty()) System.out.println("1");
			else System.out.println("0");
		}
		// front
		else if (str.equals("front")) {
			if (dq.isEmpty()) System.out.println("-1");
			else System.out.println(dq.peekFirst());
		}
		// back
		else if (str.equals("back")) {
			if (dq.isEmpty()) System.out.println("-1");
			else System.out.println(dq.peekLast());
		}
	}
	public static void main(String[] args) throws IOException {
		Main T = new Main();
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		while (n-->0) {
			String str = br.readLine();
			T.solution(str);
		}
		br.close();
	}
}