您的位置:首页 > 职场人生

元素出入栈的合法性——栈和队列面试题(4)

2016-09-18 22:53 633 查看
题目要求:元素的出,入栈顺序的合法性。如入栈{1,2,3,4,5,},出栈{4,5,3,2,1}



"test.cpp"

<strong><span style="font-size:18px;">#include<iostream>
using namespace std;
#include<assert.h>
#include<stack>

bool IsLegalOrder(int* arr1,int size1,int* arr2,int size2)
{
assert(arr1);
assert(arr2);
stack<int> s;
int j = 0;

for(int i = 0;i < size1;i++)
{
if(arr1[i] != arr2[j])
{
s.push(arr1[i]);
}
else
{
j++;
}

while(!s.empty() && s.top() == arr2[j])
{
s.pop();
j++;
}
}

if(j == size2)
{
return true;
}
return false;
}
void test()
{
int arr1[] = {1,2,3,4,5};
size_t size1 = sizeof(arr1)/sizeof(arr1[0]);
//int arr2[] = {4,5,3,2,1}; //yes
//int arr2[] = {2,4,5,3,1}; //yes
//int arr2[] = {5,4,3,2,1}; //yes
int arr2[] = {1,2,5,3,4}; //no
size_t size2 = sizeof(arr2)/sizeof(arr2[0]);

bool ret = IsLegalOrder(arr1,size1,arr2,size2);

if(ret)
{
cout<<"yes"<<endl;
}
else
{
cout<<"no"<<endl;
}
}
int main()
{
test();
return 0;
}</span></strong>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: