공부하는 히욤이

[BOJ] 10866. 덱 본문

Algorithm/BaekJoon

[BOJ] 10866. 덱

히욤이 2020. 2. 8. 05:06

BaekJoon 10866. 덱

* 문제의 저작권은 BOJ 및 문제를 만든 사람에게 있습니다.

 

 

 

 

 

[문제 접근]

ArrayList와 Deque를 이용해서 구현 해 봄

 

 

 

 

 

 

 

 

 

 

[코드]

Deque

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.Scanner;

public class Main_10866 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		//ArrayList<Integer> list = new ArrayList<>();
		Deque<Integer> deque = new ArrayDeque<>();

		for (int i = 0; i < N; i++) {
			String command = sc.next();
			switch (command) {
			case "push_back":
				deque.add(sc.nextInt());
				break;

			case "push_front":
				deque.addFirst(sc.nextInt());
				break;

			case "pop_back":
				if (!deque.isEmpty()) {
					System.out.println(deque.pollLast());
				} else {
					System.out.println("-1");
				}
				break;

			case "pop_front":
				if (!deque.isEmpty()) {
					System.out.println(deque.pollFirst());
				} else {
					System.out.println("-1");
				}
				break;

			case "size":
				System.out.println(deque.size());
				break;

			case "empty":
				if (deque.isEmpty()) {
					System.out.println("1");
				} else {
					System.out.println("0");
				}
				break;

			case "front":
				if (deque.isEmpty()) {
					System.out.println("-1");
				} else {
					System.out.println(deque.peekFirst());
				}
				break;

			case "back":
				if (deque.isEmpty()) {
					System.out.println("-1");
				} else {
					System.out.println(deque.peekLast());
				}
				break;
			}
		}
	}
}

 

 

ArrayList

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.Scanner;

public class Main{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		ArrayList<Integer> list = new ArrayList<>();
		//Deque<Integer> deque = new ArrayDeque<>();

		for (int i = 0; i < N; i++) {
			String command = sc.next();
			switch (command) {
			case "push_back":
				list.add(sc.nextInt());
				break;

			case "push_front":
				list.add(0, sc.nextInt());
				break;

			case "pop_back":
				if (!list.isEmpty()) {
					System.out.println(list.remove(list.size()-1));
				} else {
					System.out.println("-1");
				}
				break;

			case "pop_front":
				if (!list.isEmpty()) {
					System.out.println(list.remove(0));
				} else {
					System.out.println("-1");
				}
				break;

			case "size":
				System.out.println(list.size());
				break;

			case "empty":
				if (list.isEmpty()) {
					System.out.println("1");
				} else {
					System.out.println("0");
				}
				break;

			case "front":
				if (list.isEmpty()) {
					System.out.println("-1");
				} else {
					System.out.println(list.get(0));
				}
				break;

			case "back":
				if (list.isEmpty()) {
					System.out.println("-1");
				} else {
					System.out.println(list.get(list.size()-1));
				}
				break;
			}
		}
	}
}

'Algorithm > BaekJoon' 카테고리의 다른 글

[BOJ] 10799. 쇠막대기  (0) 2020.02.09
[BOJ] 17413. 단어 뒤집기 2  (0) 2020.02.09
[BOJ] 1158. 요세푸스 문제  (0) 2020.02.08
[BOJ] 10845. 큐  (0) 2020.02.07
[BOJ] 1406. 에디터  (0) 2020.02.07