一、化栈为队列
class MyQueue {
private:
//两个栈 一个实现进 一个实现出
//栈是先进后出,队列先进先出
stack<int>s1;
stack<int>s2;
int size;
public:
/** Initialize your data structure here. */
MyQueue() {
}
/** Push element x to the back of queue. */
void push(int x) {
s1.push(x);
size ++;
}
/** Removes the element from in front of queue and returns that element. */
int pop() {
if(s1.empty()) return -1;
//先把第一个push的栈弹出进入进入的栈
while(!s1.empty())
{
int tmp = s1.top();
s1.pop();
s2.push(tmp);
}
int returnVal = s2.top();
s2.pop();
while(!s2.empty())
{
int tmp = s2.top();
s2.pop();
s1.push(tmp);
}
size --;
return returnVal;
}
/** Get the front element. */
int peek() {
//返回头元素
if(s1.empty()) return -1;
//先把第一个push的栈弹出进入进入的栈
while(!s1.empty())
{
int tmp = s1.top();
s1.pop();
s2.push(tmp);
}
int returnVal = s2.top();
while(!s2.empty())
{
int tmp = s2.top();
s2.pop();
s1.push(tmp);
}
return returnVal;
}
/** Returns whether the queue is empty. */
bool empty() {
return s1.empty()? true:false;
}
};
/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue* obj = new MyQueue();
* obj->push(x);
* int param_2 = obj->pop();
* int param_3 = obj->peek();
* bool param_4 = obj->empty();
*/
总结:两个栈一个保持进一个保持出即可实现队列的功能
二、实现排序栈
class SortedStack {
private:
stack<int>s;
public:
SortedStack() {
}
void push(int val) {
stack<int>s2;
if(s.empty() || s.top() > val) s.push(val);
else
{
while(!s.empty())
{
int tmp = s.top();
if(tmp < val)
{
s.pop();
s2.push(tmp);
}
else break;
}
s.push(val);
while(!s2.empty())
{
int tmp = s2.top();
s2.pop();
s.push(tmp);
}
}
}
void pop() {
if(!s.empty()) s.pop();
}
int peek() {
return s.empty() ? -1:s.top();
}
bool isEmpty() {
return s.empty() ? true:false;
}
};
/**
* Your SortedStack object will be instantiated and called as such:
* SortedStack* obj = new SortedStack();
* obj->push(val);
* obj->pop();
* int param_3 = obj->peek();
* bool param_4 = obj->isEmpty();
*/
总结:使用两个栈来进行排序实现
三、动物收容所
class AnimalShelf {
queue<int> cat;
queue<int> dog;
public:
vector<int> res;
AnimalShelf() {
}
void enqueue(vector<int> animal) {
if(animal[1]==0)
{
cat.push(animal[0]);
}
if(animal[1]==1)
{
dog.push(animal[0]);
}
}
vector<int> dequeueAny() {
res.resize(2);
if(cat.empty()&&dog.empty())
{
res[0]=-1;
res[1]=-1;
return res;
}
if(cat.empty()&&!dog.empty())
{
res[0]=dog.front();
dog.pop();
res[1]=1;
return res;
}
if(!cat.empty()&&dog.empty())
{
res[0]=cat.front();
cat.pop();
res[1]=0;
return res;
}
if(!cat.empty()&&!dog.empty())
{
if(cat.front()<dog.front())
{
res[0]=cat.front();
cat.pop();
res[1]=0;
return res;
}
else
{
res[0]=dog.front();
dog.pop();
res[1]=1;
return res;
}
}
return res;
}
vector<int> dequeueDog() {
res.resize(2);
if(!dog.empty())
{
res[0]=dog.front();
dog.pop();
res[1]=1;
return res;
}
else
{
res[0]=-1;
res[1]=-1;
return res;
}
}
vector<int> dequeueCat() {
res.resize(2);
if(!cat.empty())
{
res[0]=cat.front();
cat.pop();
res[1]=0;
return res;
}
else
{
res[0]=-1;
res[1]=-1;
return res;
}
}
};
/**
* Your AnimalShelf object will be instantiated and called as such:
* AnimalShelf* obj = new AnimalShelf();
* obj->enqueue(animal);
* vector<int> param_2 = obj->dequeueAny();
* vector<int> param_3 = obj->dequeueDog();
* vector<int> param_4 = obj->dequeueCat();
*/
总结:两个队列
四、最小高度树
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* sortedArrayToBST(vector<int>& nums) {
//此题看到题目很容易想到递归方法。
//递归分治
if(nums.size() == 0) return NULL;
return estaTree(nums, 0, nums.size());
}
TreeNode *estaTree(vector<int>nums, int left, int right)
{
if(left >= right) return NULL;
int mid = (left + right) / 2;
TreeNode *node = new TreeNode(nums[mid]);
node->left = estaTree(nums, left, mid);
node->right = estaTree(nums, mid + 1, right);
return node;
}
};
总结:由于要构造二叉树,很容易想到要进行递归或者使用栈实现。递归分支