ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ฉ๐ป๐ป ๋ฌธ์
โ๐ป ํ์ด
๐จ Go
// https://www.acmicpc.net/problem/10866
// ๋ฑ์ ๊ฐ๋
์ ์ตํ๊ณ ์ค์ตํ๋ ๋ฌธ์
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
reader := bufio.NewReader(os.Stdin)
writer := bufio.NewWriter(os.Stdout)
defer writer.Flush()
var n int
fmt.Fscanln(reader, &n)
var deque []int
for i := 0; i < n; i++ {
var command string
var num int
fmt.Fscanln(reader, &command, &num)
if command == "push_back" {
deque = pushBack(deque, num)
} else if command == "push_front" {
deque = pushFront(deque, num)
} else if command == "pop_front" {
var front int
deque, front = popFront(deque)
fmt.Fprintln(writer, front)
} else if command == "pop_back" {
var back int
deque, back = popBack(deque)
fmt.Fprintln(writer, back)
} else if command == "size" {
fmt.Fprintln(writer, size(deque))
} else if command == "empty" {
if isEmpty(deque) {
fmt.Fprintln(writer, 1)
} else {
fmt.Fprintln(writer, 0)
}
} else if command == "front" {
fmt.Fprintln(writer, front(deque))
} else if command == "back" {
fmt.Fprintln(writer, back(deque))
}
}
}
func pushBack(deque []int, num int) []int {
deque = append(deque, num)
return deque
}
func pushFront(deque []int, num int) []int {
deque = append([]int{num}, deque...)
return deque
}
func popFront(deque []int) ([]int, int) {
if isEmpty(deque) {
return deque, -1
}
front := deque[0]
deque = deque[1:]
return deque, front
}
func popBack(deque []int) ([]int, int) {
if isEmpty(deque) {
return deque, -1
}
back := deque[len(deque)-1]
deque = deque[:len(deque)-1]
return deque, back
}
func size(deque []int) int {
return len(deque)
}
func isEmpty(deque []int) bool {
if size(deque) == 0 {
return true
}
return false
}
func front(deque []int) int {
if isEmpty(deque) {
return -1
}
return deque[0]
}
func back(deque []int) int {
if isEmpty(deque) {
return -1
}
return deque[len(deque)-1]
}
๐จ Python3
# https://www.acmicpc.net/problem/10866
# ๋ฑ์ ๊ฐ๋
์ ์ตํ๊ณ ์ค์ตํ๋ ๋ฌธ์
import sys
class Deque:
def __init__(self):
self.list = []
def push_front(self, x):
self.list.insert(0, x)
def push_back(self, x):
self.list.append(x)
def pop_front(self):
if self.is_empty():
return -1
return self.list.pop(0)
def pop_back(self):
if self.is_empty():
return -1
return self.list.pop(-1)
def size(self):
return len(self.list)
def is_empty(self):
if self.size() == 0:
return True
return False
def front(self):
if self.size() == 0:
return -1
return self.list[0]
def back(self):
if self.size() == 0:
return -1
return self.list[-1]
if __name__ == "__main__":
n = int(sys.stdin.readline())
deque = Deque()
for i in range(n):
inputs = sys.stdin.readline().split()
command = inputs[0]
if command == "push_back":
deque.push_back(inputs[1])
elif command == "push_front":
deque.push_front(inputs[1])
elif command == "front":
print(deque.front())
elif command == "back":
print(deque.back())
elif command == "size":
print(deque.size())
elif command == "empty":
if deque.is_empty():
print(1)
else:
print(0)
elif command == "pop_front":
print(deque.pop_front())
elif command == "pop_back":
print(deque.pop_back())
728x90
'dev > algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ / 1920๋ฒ / ์ ์ฐพ๊ธฐ [Go][Python3] (0) | 2020.11.17 |
---|---|
BOJ / 1021๋ฒ / ํ์ ํ๋ ํ [Go][Python3] (0) | 2020.11.14 |
BOJ / 1966๋ฒ / ํ๋ฆฐํฐ ํ [Go][Python3] (0) | 2020.11.12 |
BOJ / 11866๋ฒ / ์์ธํธ์ค ๋ฌธ์ 0 [Go][Python3] (0) | 2020.11.11 |
BOJ / 2164๋ฒ / ์นด๋2 [Go][Python3] (0) | 2020.11.10 |
๋๊ธ
๊ธ ๋ณด๊ดํจ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ
TAG
- ๋ฐฑ์ค
- ballet
- ์๋ฐ
- ํ
- Algorithm
- ๋ธ๋ฃจํธํฌ์ค
- java
- ๋งฅ๋ถ
- dfs
- ๋งฅ๋ถ ์ ๊ทธ๋ ์ด๋
- MongoDB
- ๋ฐ๋
- ๋งฅ๋ถํ๋ก
- ์๊ฐ๊ต์ฒด
- ํ๋ก์ด๋์์ฌ
- ์๊ณ ๋ฆฌ์ฆ
- AWS
- ์ด๋ถํ์
- ๋ถํ ์ ๋ณต
- go
- ์คํ
- BOJ
- Golang
- dp
- Macbook pro 2012 mid 13
- BFS
- ๋ชฝ๊ณ ๋๋น
- baekjoon
- ํด์๋งต
- python3
- Total
- Today
- Yesterday