[BaekJoon] 10866번 - 덱 [Java][C++]
[BaekJoon] 10866번 - 덱 [Java][C++]
1. 아이디어
덱 자료구조를 구현하는 문제로 배열을 활용해서 구현했다. 덱은 양방향 삽입, 삭제가 모두 가능하므로 배열의 중간을 포인터의 위치로 잡아줬다.
2. 코드
1. 풀이 [Java]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import java.io.*;
import java.util.*;
public class Main {
static final int MX = 20000;
static int[] deque = new int[MX];
static int front = MX / 2, rear = MX / 2;
static void push_front(int x) {
deque[--front] = x;
}
static void push_back(int x) {
deque[rear++] = x;
}
static int pop_front() {
return deque[front++];
}
static int pop_back() {
return deque[--rear];
}
static int size() {
return rear - front;
}
static boolean empty() {
return front == rear;
}
static int front() {
return deque[front];
}
static int back() {
return deque[rear - 1];
}
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());
while (n-- > 0) {
st = new StringTokenizer(br.readLine());
String c = st.nextToken();
if (c.equals("push_front")) {
int x = Integer.parseInt(st.nextToken());
push_front(x);
} else if (c.equals("push_back")) {
int x = Integer.parseInt(st.nextToken());
push_back(x);
} else if (c.equals("pop_front")) {
if (empty()) {
sb.append("-1\n");
} else {
sb.append(pop_front()).append("\n");
}
} else if (c.equals("pop_back")) {
if (empty()) {
sb.append("-1\n");
} else {
sb.append(pop_back()).append("\n");
}
} else if (c.equals("size")) {
sb.append(size()).append("\n");
} else if (c.equals("empty")) {
if (empty()) {
sb.append("1\n");
} else {
sb.append("0\n");
}
} else if (c.equals("front")) {
if (empty()) {
sb.append("-1\n");
} else {
sb.append(front()).append("\n");
}
} else {
if (empty()) {
sb.append("-1\n");
} else {
sb.append(back()).append("\n");
}
}
}
System.out.println(sb);
}
}
2. 풀이 [C++]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <bits/stdc++.h>
using namespace std;
const int MX = 20000;
int dq[MX];
int s = MX / 2, e = MX / 2;
void push_front(int x) {
dq[--s] = x;
}
void push_back(int x) {
dq[e++] = x;
}
void pop_front() {
s++;
}
void pop_back() {
e--;
}
int size() {
return e - s;
}
bool empty() {
return s == e;
}
int front() {
return dq[s];
}
int back() {
return dq[e - 1];
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
while (n--) {
string c;
cin >> c;
if (c == "push_front") {
int x;
cin >> x;
push_front(x);
} else if (c == "push_back") {
int x;
cin >> x;
push_back(x);
} else if (c == "pop_front") {
if (empty()) {
cout << -1 << '\n';
} else {
cout << front() << '\n';
pop_front();
}
} else if (c == "pop_back") {
if (empty()) {
cout << -1 << '\n';
} else {
cout << back() << '\n';
pop_back();
}
} else if (c == "size") {
cout << size() << '\n';
} else if (c == "empty") {
cout << empty() << '\n';
} else if (c == "front") {
if (empty()) {
cout << -1 << '\n';
} else {
cout << front() << '\n';
}
} else {
if (empty()) {
cout << -1 << '\n';
} else {
cout << back() << '\n';
}
}
}
}
3. 디버깅
없음.
4. 참고
없음.
This post is licensed under CC BY 4.0 by the author.