栈、队列系列

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wk_bjut_edu_cn/article/details/83749783

 剑指Offer(9)--用两个栈实现队列(用两个队列实现栈)

思路:

当需要输出的时候,如果pop栈中有数据就直接输出栈顶元素,如果没有的话,将push栈中的数据全部倒入pop,然后将pop栈顶的数据输出。 

class TwoStacksQueue
{
public:
	void push(int obj);
	int poll();
	int peek();
private:
	stack<int> stackPush;
	stack<int> stackPop;
};
void TwoStacksQueue::push(int obj)
{
	stackPush.push(obj);
}
int TwoStacksQueue::poll()
{
	if (stackPush.empty() && stackPop.empty())
		return -1;
	if (stackPop.empty())
	{
		while (!stackPush.empty())
		{
			stackPop.push(stackPush.top());
			stackPush.pop();
		}
	}
	int temp = stackPop.top();
	stackPop.pop();
	return temp;
}
//看一下,并不需要取走
int TwoStacksQueue::peek()
{
	if (stackPush.empty() && stackPop.empty())
		return -1;
	if (stackPop.empty())
	{
		while (!stackPush.empty())
		{
			stackPop.push(stackPush.top());
			stackPush.pop();
		}
	}
	int temp = stackPop.top();
	return temp;
}

两个队列实现栈 

#include<iostream>
#include<queue>
using namespace std;
//如何仅用队列结构实现栈结构

class TwoQueuesStack {
public:
	void push(int obj);
	int pop();
	int peek();
private:
	queue<int> que;
	queue<int> help;
};
void TwoQueuesStack::push(int obj)
{
	que.push(obj);
}
int TwoQueuesStack::pop()
{
	if (que.empty())
		return -1;
	while (que.size() != 1)
	{
		help.push(que.front());
		que.pop();
	}
	int temp = que.front();
	que.pop();
	que.swap(help);
	return temp;
}

 剑指Offer(30)--包含min函数的栈 

定义栈的数据结构,请在该类型中实现一个能够得到栈的最下元素的min函数。在该栈中,调用min、push及pop的时间复杂度都为O(1)

思路:
准备两个栈,一个用于正常放置元素的栈data,另一个min栈存放当前最小元素,在压栈的过程中min栈随着data栈增长,压入
data栈的元素和min栈的栈顶比较,如果待压入的数比min栈的栈顶要小,那么min栈中也压这个数;否则,重复压入min栈的栈顶
弹出的时候,同步弹出即可

class MyStack {
public:
	void push(int obj);
	int pop();
	int getmin();
private:
	stack<int> stackMin;
	stack<int> stackData;
};
void MyStack::push(int obj)
{
	if (stackMin.empty() || obj < getmin())
		stackMin.push(obj);
	else
	{
		int min = stackMin.top();
		stackMin.push(min);
	}
	stackData.push(obj);
}
int MyStack::pop()
{
	if (stackData.empty())
		return -1;
	stackMin.pop();
	int temp = stackData.top();
	stackData.pop();
	return temp;
}
int MyStack::getmin()
{
	if (stackMin.empty())
		return -1;
	return stackMin.top();
}

 剑指Offer(31)--栈的压入、弹出序列

输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。

思路:

建立一个辅助栈s,把输入的第一个序列中的数字依次压入该辅助栈,并按照第二个序列的顺序依次从该栈中弹出数字。

判断第二个序列待输出的数字是否是栈顶元素,如果不是,则从输入序列中依次输入栈中,如果一直没找到,则返回为false。

#include<iostream>
#include<stack>
using namespace std;
bool IsPopOrder(const int* Push, const int *Pop, int length)
{
	if (Push == nullptr || Pop == nullptr || length < 1)
		return false;
	const int* nextPush = Push;
	const int* nextPop = Pop;
	stack<int> sData;
	//只要弹出序列不为空,一直循环
	while (nextPop - Pop < length)
	{
		while (sData.empty() || sData.top()!=*nextPop)
		{
			//输入序列中没有元素了
			if (nextPush - Push == length)
				break;
			sData.push(*nextPush);
			++nextPush;
		}
		if (sData.top() != *nextPop)
			break;
		sData.pop();
		++nextPop;
	}
	if (sData.empty() && nextPop - Pop == length)
		return true;
	return false;
}

 剑指Offer(59)--队列的最大值

给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值。例如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3,那么一共存在6个滑动窗口,他们的最大值分别为{4,4,6,6,6,5}; 针对数组{2,3,4,2,6,2,5,1}的滑动窗口有以下6个: {[2,3,4],2,6,2,5,1}, {2,[3,4,2],6,2,5,1}, {2,3,[4,2,6],2,5,1}, {2,3,4,[2,6,2],5,1}, {2,3,4,2,[6,2,5],1}, {2,3,4,2,6,[2,5,1]}

#include<iostream>
#include<vector>
#include<deque>
using namespace std;
vector<int> MaxInWindow(const vector<int>& num, int size)
{
	vector<int> maxInwindows;
	if (num.size() >= size && size >= 1)
	{
		//链表头部存放当前的最大值
		deque<int> index;
		for (int i = 0; i < size; ++i)
		{
			while (!index.empty() && num[i] >= num[index.back()])
				index.pop_back();
			index.push_back(i);
		}
		for (int i = size; i < num.size(); ++i)
		{
			//存放窗口的最大值
			maxInwindows.push_back[num[index.front()]];
			while (!index.empty() && num[i] >= num[index.back()])
				index.pop_back();
			if (!index.empty() && index.front() <= (i - size))
				index.pop_front();
			//不论大小都会先存入链表中,因为有可能是后面值中的最大值
			index.push_back(i);
		}
		maxInwindows.push_back(num[index.front()]);
	}
	return maxInwindows;
}

猜你喜欢

转载自blog.csdn.net/wk_bjut_edu_cn/article/details/83749783