入门题目
栈实现队列
很简单,一堆数放到栈里面,先进后出,比如是12345,然后把他们再丢到一个栈里面,此时栈的顺序是54321,然后再出来的顺序就又成了是12345。
需要注意的是pop的时候,要一次把当前in里面的全部拿出来不能留v
type MyQueue struct {
in []int
out []int
}
func Constructor() MyQueue {
return MyQueue{}
}
func (this *MyQueue) Push(x int) {
this.in = append(this.in, x)
}
func (this *MyQueue) Pop() int {
if len(this.out) == 0 {
for len(this.in) != 0 {
this.out = append(this.out, this.in[len(this.in)-1])
this.in = this.in[:len(this.in)-1]
}
}
res := this.out[len(this.out)-1]
this.out = this.out[:len(this.out)-1]
return res
}
func (this *MyQueue) Peek() int {
if len(this.out) == 0 {
for len(this.in) != 0 {
this.out = append(this.out, this.in[len(this.in)-1])
this.in = this.in[:len(this.in)-1]
}
}
return this.out[len(this.out)-1]
}
func (this *MyQueue) Empty() bool {
return len(this.out) == 0 && len(this.in) == 0
}
队列实现栈
很简单,每次把元素压入队列种就让前面的元素重新进入队列,就相当于把前进的又放到了后面,从而实现了先进后出。
type MyStack struct {
q []int
}
func Constructor() MyStack {
return MyStack{}
}
func (this *MyStack) Push(x int) {
n := len(this.q)
this.q = append(this.q, x)
for i := 0; i < n; i++ {
this.q = append(this.q, this.q[i])
}
this.q = this.q[n:]
}
func (this *MyStack) Pop() int {
res := this.q[0]
this.q = this.q[1:]
return res
}
func (this *MyStack) Top() int {
return this.q[0]
}
func (this *MyStack) Empty() bool {
if len(this.q) == 0 {
return true
}
return false
}
最小栈
题干:设计一个支持 push
,pop
,top
操作,并能在常数时间内检索到最小元素的栈。
实现 MinStack
类:
-
MinStack()
初始化堆栈对象。 -
void push(int val)
将元素val推入堆栈。 -
void pop()
删除堆栈顶部的元素。 -
int top()
获取堆栈顶部的元素。 -
int getMin()
获取堆栈中的最小元素。
用另外一个栈来存储最小值,和原栈同步压入。感觉很难想到,那就背下来吧。
type MinStack struct {
stack []int
top int
min []int
}
func Constructor() MinStack {
return MinStack{}
}
func (this *MinStack) Push(val int) {
this.stack = append(this.stack, val)
if this.top == 0 {
this.min = append(this.min, val)
} else if val < this.min[this.top-1] {
this.min = append(this.min, val)
} else {
this.min = append(this.min, this.min[this.top-1])
}
this.top++
}
func (this *MinStack) Pop() {
if this.top == 0 {
return
}
this.top--
this.stack = this.stack[:this.top]
this.min = this.min[:this.top]
}
func (this *MinStack) Top() int {
return this.stack[this.top-1]
}
func (this *MinStack) GetMin() int {
return this.min[this.top-1]
}
双端队列
题干:设计实现双端队列。
实现 MyCircularDeque
类:
MyCircularDeque(int k)
:构造函数,双端队列最大为k
。boolean insertFront()
:将一个元素添加到双端队列头部。 如果操作成功返回true
,否则返回false
。boolean insertLast()
:将一个元素添加到双端队列尾部。如果操作成功返回true
,否则返回false
。boolean deleteFront()
:从双端队列头部删除一个元素。 如果操作成功返回true
,否则返回false
。boolean deleteLast()
:从双端队列尾部删除一个元素。如果操作成功返回true
,否则返回false
。int getFront()
):从双端队列头部获得一个元素。如果双端队列为空,返回-1
。int getRear()
:获得双端队列的最后一个元素。 如果双端队列为空,返回-1
。boolean isEmpty()
:若双端队列为空,则返回true
,否则返回false
。boolean isFull()
:若双端队列满了,则返回true
,否则返回false
。
双链表
package main
type Node struct {
val int
last *Node
next *Node
}
type MyCircularDeque struct {
head *Node
tail *Node
size int
limite int
}
func Constructor(k int) MyCircularDeque {
return MyCircularDeque{
head: nil,
tail: nil,
size: 0,
limite: k,
}
}
func (this *MyCircularDeque) InsertFront(value int) bool {
if this.IsFull() {
return false
}
if this.IsEmpty() {
node := &Node{val: value}
this.head = node
this.tail = node
this.size++
return true
} else {
node := &Node{val: value}
this.head.last = node
node.next = this.head
node.last = nil
this.head = node
this.size++
return true
}
}
func (this *MyCircularDeque) InsertLast(value int) bool {
if this.IsFull() {
return false
}
if this.IsEmpty() {
node := &Node{val: value}
this.head = node
this.tail = node
this.size++
return true
} else {
node := &Node{val: value}
this.tail.next = node
node.last = this.tail
node.next = nil
this.tail = node
this.size++
return true
}
}
func (this *MyCircularDeque) DeleteFront() bool {
if this.IsEmpty() {
return false
}
this.head = this.head.next
if this.head == nil {
this.size--
} else {
this.head.last = nil
this.size--
}
return true
}
func (this *MyCircularDeque) DeleteLast() bool {
if this.IsEmpty() {
return false
}
this.tail = this.tail.last
if this.tail == nil {
this.size--
} else {
this.tail.next = nil
this.size--
}
return true
}
func (this *MyCircularDeque) GetFront() int {
if this.IsEmpty() {
return -1
}
return this.head.val
}
func (this *MyCircularDeque) GetRear() int {
if this.IsEmpty() {
return -1
}
return this.tail.val
}
func (this *MyCircularDeque) IsEmpty() bool {
return this.size == 0
}
func (this *MyCircularDeque) IsFull() bool {
return this.size == this.limite
}
固定数组(循环队列)
package main
import "fmt"
type MyCircularDeque struct {
q []int
front int
rear int
size int
limite int
}
func Constructor(k int) MyCircularDeque {
return MyCircularDeque{
q: make([]int, k),
front: 0,
rear: 0,
size: 0,
limite: k,
}
}
func (this *MyCircularDeque) InsertFront(value int) bool {
if this.IsFull() {
return false
}
if this.IsEmpty() {
this.front, this.rear = 0, 0
this.q[this.front] = value
this.size++
return true
}
this.front = (this.front + this.limite - 1) % this.limite
this.q[this.front] = value
this.size++
return true
}
func (this *MyCircularDeque) InsertLast(value int) bool {
if this.IsFull() {
return false
}
if this.IsEmpty() {
this.front, this.rear = 0, 0
this.q[this.front] = value
this.size++
return true
}
this.rear = (this.rear + this.limite + 1) % this.limite
this.q[this.rear] = value
this.size++
return true
}
func (this *MyCircularDeque) DeleteFront() bool {
if this.IsEmpty() {
return false
}
this.size--
this.front = (this.front + 1) % this.limite
return true
}
func (this *MyCircularDeque) DeleteLast() bool {
if this.IsEmpty() {
return false
}
this.size--
this.rear = (this.rear - 1) % this.limite
return true
}
func (this *MyCircularDeque) GetFront() int {
if this.IsEmpty() {
return -1
}
return this.q[this.front]
}
func (this *MyCircularDeque) GetRear() int {
if this.IsEmpty() {
return -1
}
return this.q[this.rear]
}
func (this *MyCircularDeque) IsEmpty() bool {
return this.size == 0
}
func (this *MyCircularDeque) IsFull() bool {
return this.size == this.limite
}