C++栈(出栈序列判断)

题目描述

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

结果

运行时间:4ms

占用内存:480k

知识点:vector

  • vector定义
  • vector基本函数
  • vector实例
  • vector 和同类型数据结构比较
前面博客写的很水!!真的很水!!此博以后必然认知!!!你看我发现我打错了个字!是认真!!!
这个总结虽然也是没有什么逻辑,但是后边会更加完整且逻辑性强,便于理解、阅读、学习·····
vector<int> a;
/*
Vector<类型>标识符
Vector<类型>标识符(最大容量)
Vector<类型>标识符(最大容量,初始所有值)
Int i[5]={1,2,3,4,5} 
Vector<类型>v i(I,i+2);//得到i索引值为3以后的值
Vector< vector< int> >v; 二维向量//这里最外的<>要有空格。否则在比较旧的编译器下无法通过
*/
//vector 
/*
向量(Vector)是一个封装了动态大小数组的顺序容器(Sequence Container)。跟任意其它类型容器一样,它能够存放各种类型的对象。可以简单的认为,向量是一个能够存放任意类型的动态数组。
*/
	//容器:用来组织和存储其它对象的对象
	/*
	容器特性:顺序序列、动态数组、能够感知内存分配器(Allocator-aware)(容器使用一个内存分配器对象来动态地处理它的存储需求)
	*/
//基本函数实现
/*
void push_back(const T& x):向量尾部增加一个元素X
void pop_back():删除向量中最后一个元素
bool empty() const:判断向量是否为空,若为空,则向量中无元素
int size() const:返回向量中元素的个数
void swap(vector&):交换两个同类型向量的数据
*/

实例1删除插入

pop_back()&push_back(elem)实例在容器最后移除和插入数据

#include <string.h>
#include <vector>
#include <iostream>
using namespace std;
int main()
{
    vector<int>obj;//创建一个向量存储容器 int
    for(int i=0;i<10;i++) // push_back(elem)在数组最后添加数据 
    {
        obj.push_back(i);
        cout<<obj[i]<<",";    
    }
    for(int i=0;i<5;i++)//去掉数组最后一个数据 
    {
        obj.pop_back();
    }
     cout<<"\n"<<endl;
    for(int i=0;i<obj.size();i++)//size()容器中实际数据个数 
    {
        cout<<obj[i]<<",";
    }
     return 0;
}
/*输出结果:
0,1,2,3,4,5,6,7,8,9,
0,1,2,3,4,
*/

实例2顺逆排序

排序

#include <string.h>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std; 
int main()
{
    vector<int>obj;
     obj.push_back(1);
    obj.push_back(3);
    obj.push_back(0); 
    sort(obj.begin(),obj.end());//从小到大 
    cout<<"从小到大:"<<endl;
    for(int i=0;i<obj.size();i++)
    {
        cout<<obj[i]<<",";  
    }  
    cout<<"\n"<<endl; 
    cout<<"从大到小:"<<endl;
    reverse(obj.begin(),obj.end());//从大到小 
    for(int i=0;i<obj.size();i++)
    {
        cout<<obj[i]<<",";
    }
    return 0;
}
/*输出结果:
从小到大:
0,1,3,
从大到小:
3,1,0,
*/

实例3定义数组

定义二维数组

#include <vector>
using namespace std;
int main()
{
    int N=5, M=6; 
    vector<vector<int> > obj(N); //定义二维动态数组大小5行
}
int main()
{
    int N=5, M=6; 
    vector<vector<int> > obj(N, vector<int>(M)); //定义二维动态数组5行6列 
}

实例4反转

//如果不是改变原来的容器,而是翻转之后放在新容器里面,直接用reverse
#include <iostream>    
#include <algorithm>   
#include <vector>    
//reverse 函数原型
template <class BidirectionalIterator> void reverse (BidirectionalIterator first, BidirectionalIterator last)
{
     while ((first!=last)&&(first!=--last))
     {
          std::iter_swap (first,last);
          ++first;
      }
}
 int main () {
    std::vector<int> myvector;
    for (int i=1; i<10; ++i) myvector.push_back(i);   // 1 2 3 4 5 6 7 8 9
    std::reverse(myvector.begin(),myvector.end());    // 9 8 7 6 5 4 3 2 1
    std::cout << "翻转结果:";
    for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
        std::cout << ' ' << *it;
    std::cout << '\n';
    return 0;
}
/*输出结果:
 9 8 7 6 5 4 3 2 1
*/

知识点:栈

  • 栈定义
  • 栈操作
  • 栈和队比较

代码

class Solution {
public:
    bool IsPopOrder(vector<int> pushV,vector<int> popV) {
        /*
        pushV压栈序列 popV可能的弹栈序列 (可能边压栈边弹栈,也可能压栈结束弹栈)
        定义一个临时栈,将将压栈顺序压入;然后出栈,根据出栈序列将压入的临时栈元素弹出。
        如果能够将临时栈中序列都弹出,那么是该栈的弹出序列,如果不能那么不是!!!
        */
        stack<int>temp_stack;      //定义一个临时栈,用来比较
        int k=pushV.size();
        if(k==0)return false;
        int j=0;
        for(int i=0;i<=k;i++)
        {
            temp_stack.push(pushV[i]);
            while(!temp_stack.empty()&&temp_stack.top()==popV[j])//很关键!!!出栈判断,栈不空,栈顶元素和出栈序列对应!
            {
                temp_stack.pop();
                j++;               //有相同的就把栈弹出来,数组遍历下一个
            }
        }
        if(!temp_stack.empty()){
            return false;
        }
        else
            return true;
    }
};

总结

1)这个题还是很油的!解题方法(你出栈那我就模拟出栈喽!这波操作很溜!)学习了!
2)vector 容器、模版、数组······数据结构间的关系,转化一瞬间都忘了!!!

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
出栈序列判断可以利用一个辅助来模拟整个出栈的过程,下面是一个基于链实现的C++代码示例: ```c++ #include <iostream> using namespace std; // 链结构体 struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; class LinkedStack { public: LinkedStack() { head = new ListNode(-1); } // 入 void push(int x) { ListNode *node = new ListNode(x); node->next = head->next; head->next = node; } // 出栈 int pop() { if (isEmpty()) { return -1; } ListNode *node = head->next; int val = node->val; head->next = node->next; delete node; return val; } // 判断是否为空 bool isEmpty() { return head->next == NULL; } // 获取顶元素 int top() { if (isEmpty()) { return -1; } return head->next->val; } private: ListNode *head; }; // 判断出栈序列是否合法 bool isValidPopOrder(int *pushOrder, int *popOrder, int n) { if (pushOrder == NULL || popOrder == NULL || n <= 0) { return false; } LinkedStack stack; int pushIndex = 0, popIndex = 0; while (popIndex < n) { while (stack.isEmpty() || stack.top() != popOrder[popIndex]) { if (pushIndex >= n) { return false; } stack.push(pushOrder[pushIndex++]); } stack.pop(); popIndex++; } return true; } int main() { int pushOrder[] = {1, 2, 3, 4, 5}; int popOrder[] = {4, 5, 3, 2, 1}; if (isValidPopOrder(pushOrder, popOrder, 5)) { cout << "The pop order is valid" << endl; } else { cout << "The pop order is invalid" << endl; } return 0; } ``` 这里我们利用辅助模拟整个出栈的过程,具体实现见 `isValidPopOrder` 函数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值