队列(Queue)也是一种抽象数据类型,其同样定义了两个基本操作,入队(enqueue)以及出队(dequeue),其遵循先进先出原则(FIFO, first in, first out),就像我们去买火车票排的那个队列一样,遵循先来后到,先来的先买,买完后出队(从队头删除),后来的自动站在队尾(在队尾添加)。
inttop(bool pop_last) { int from = q[0].empty() ? 1 : 0; int to = 1 - from; int size = q[from].size(); for (int i = 0; i < size - 1; ++i) { q[to].push(q[from].front()); q[from].pop(); } int ret = q[from].front(); if (!pop_last) { q[to].push(ret); } q[from].pop(); return ret; } public: /** Initialize your data structure here. */ MyStack() {
}
/** Push element x onto stack. */ voidpush(int x) { (q[0].empty() ? q[1] : q[0]).push(x); }
/** Removes the element on top of the stack and returns that element. */ intpop() { returntop(true); }
/** Get the top element. */ inttop(){ returntop(false); }
/** Returns whether the stack is empty. */ boolempty(){ return q[0].empty() && q[1].empty(); } };
这种算法应当是低效的,因为在 pop 或者 top 的过程中需要在两个队列之间大量的移动数据。但是在测试中运行效率依旧超过 100% 的人。在这种情况下 push 操作的复杂度为 O(1),pop 和 top 操作的复杂度为 O(N),其官方给出了三种解决方案,其中两种解决方案的 pop 的复杂度为 O(N) 而 push 的复杂度为 O(1),还有一种解决方案 push 的复杂度为 O(N),而 pop 的复杂度为 O(1),两种方案的选择可取决于此栈是需要进行频繁的读还是频繁的写操作,如果需要进行频繁的读操作(top 或 pop)可使用 pop 复杂度为 O(1) 的方案,如需进行频繁的写操作(push)可使用 push 复杂度为 O(1) 的解决方案,可自行查看。
0x04 N 个不同元素依次进栈可得到的出栈序列个数
这个是个公式,卡特兰数(Catalan number),公式如下:
S=n+11C2nn
其中 n 为 n 个不同元素依次入栈,S 为可得到的不同的出栈序列的个数。
这个公式可以用数学归纳法证明,证明过程过于复杂,在此不做赘述。
0x02 队列(Queue)
0x00 顺序存储结构实现循环队列
从一道 LeetCode 题目说起,622. Design Circular Queue,这道题所要求设的 class 中,在构造函数里将会传入一个队列的最大的大小,这样我们就可以使用数组来实现,而无需担心其会溢出,代码如下:
classMyCircularQueue { int *a; int front, rear, size, capacity; public: /** Initialize your data structure here. Set the size of the queue to be k. */ MyCircularQueue(int k) : a(newint[k]), front(0), rear(-1), size(0), capacity(k) { }
/** Insert an element into the circular queue. Return true if the operation is successful. */ boolenQueue(int value){ if (isFull()) { returnfalse; } ++size; a[rear = (rear + 1) % capacity] = value; returntrue; }
/** Delete an element from the circular queue. Return true if the operation is successful. */ booldeQueue(){ if (isEmpty()) { returnfalse; } --size; front = (front + 1) % capacity; returntrue; }
/** Get the front item from the queue. */ intFront(){ returnisEmpty() ? -1 : a[front]; }
/** Get the last item from the queue. */ intRear(){ returnisEmpty() ? -1 : a[rear]; }
/** Checks whether the circular queue is empty or not. */ boolisEmpty(){ return size == 0; }
/** Checks whether the circular queue is full or not. */ boolisFull(){ return size == capacity; } };
classMyCircularDeque { int *a; int front, rear, size, capacity; public: /** Initialize your data structure here. Set the size of the deque to be k. */ MyCircularDeque(int k) : a(newint[k]), front(0), rear(-1), size(0), capacity(k) { }
/** Adds an item at the front of Deque. Return true if the operation is successful. */ boolinsertFront(int value){ if (isFull()) { returnfalse; } ++size; if (front - 1 < 0) { a[front = capacity - 1] = value; } else { a[--front] = value; } rear = (front + size - 1) % capacity; returntrue; }
/** Adds an item at the rear of Deque. Return true if the operation is successful. */ boolinsertLast(int value){ if (isFull()) { returnfalse; } ++size; a[rear = (rear + 1) % capacity] = value; returntrue; }
/** Deletes an item from the front of Deque. Return true if the operation is successful. */ booldeleteFront(){ if (isEmpty()) { returnfalse; } --size; front = (front + 1) % capacity; returntrue; }
/** Deletes an item from the rear of Deque. Return true if the operation is successful. */ booldeleteLast(){ if (isEmpty()) { returnfalse; } --size; if (rear - 1 < 0) { rear = capacity - 1; } else { --rear; } returntrue; }
/** Get the front item from the deque. */ intgetFront(){ returnisEmpty() ? -1 : a[front]; }
/** Get the last item from the deque. */ intgetRear(){ returnisEmpty() ? -1 : a[rear]; }
/** Checks whether the circular deque is empty or not. */ boolisEmpty(){ return size == 0; }
/** Checks whether the circular deque is full or not. */ boolisFull(){ return size == capacity; } };
classMyQueue { std::stack<int> stk1, stk2; public: /** Initialize your data structure here. */ MyQueue() {
}
/** Push element x to the back of queue. */ voidpush(int x){ while (!stk1.empty()) { stk2.push(stk1.top()); stk1.pop(); } stk2.push(x); while (!stk2.empty()) { stk1.push(stk2.top()); stk2.pop(); } }
/** Removes the element from in front of queue and returns that element. */ intpop(){ int t = stk1.top(); stk1.pop(); return t; }
/** Get the front element. */ intpeek(){ return stk1.top(); }
/** Returns whether the queue is empty. */ boolempty(){ return stk1.empty(); } };
插入操作(push)需要 O(N) 的复杂度,pop 和 top 操作仅需要 O(1) 的复杂度,这道题同样有官方的 solution,check out 一下,官方同样给出了两种方法,第一种方法和我的一样,push 在 O(N) 的复杂度,以及 O(1) 的 pop 和 top 操作。