面试题31:栈的压入、弹出序列

面试题31:栈的压入、弹出序列

题目

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

考点

栈模拟

书本版本

#include<stdio.h>
#include<string.h>
#define ERRCODE 0
bool IsPopOrder(const int *pPush, const int *pPop, int length)
{
    if(pPush == nullptr || pPop == nullptr || length <= 0) return false;
    int *temp = new int[length];
    memset(temp,ERRCODE,length);
    int tempIdx = 0, pushIdx = 0, popIdx = 0;
    
    while (true)
    {
        if(tempIdx == 0){
            while(pPush[pushIdx] != pPop[popIdx]){
                temp[tempIdx++] = pPush[pushIdx++];
                if(pushIdx == length) return false; 
            }
            temp[tempIdx] = pPush[pushIdx];
        }
        if(temp[tempIdx] == pPop[popIdx]){
            temp[tempIdx] = ERRCODE;
            tempIdx --;
            popIdx ++;
            if(tempIdx == -1 && popIdx == length) return true;
            continue;
        }
        if(temp[tempIdx] != pPop[popIdx]){
            if(++pushIdx == length) return false;
            while(pPush[pushIdx] != pPop[popIdx]){
                temp[++tempIdx] = pPush[pushIdx++];
                if(pushIdx == length) return false;
            }
        }

    }
}

LeetCode版本(推荐)

class Solution {
public:
    bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
        stack<int> temp;
        int popidx =0;
        for(auto num: pushed){
            temp.push(num);
            while(!temp.empty() && temp.top() == popped[popidx]){
                popidx ++;
                temp.pop();
            }
        }
        return temp.empty();
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
非常感谢您的提问,以下是我的回答: 1. Spring Boot的自动配置原理: Spring Boot的自动配置是基于条件注解实现的。Spring Boot会根据classpath中的jar包、类、属性等信息,来决定是否需要给容器中添加配置类。如果需要添加,则会根据条件自动配置底层框架,如Tomcat、Redis、MySQL等。如果不需要添加,则不会添加。 2. Spring Boot导入其他的配置文件: Spring Boot可以通过在application.properties或application.yml文件中添加spring.config.import属性来导入其他的配置文件。例如,可以添加以下内容来导入另一个配置文件: ``` spring.config.import=classpath:/another-config.yml ``` 3. Spring Boot的核心注解: Spring Boot的核心注解包括@SpringBootApplication、@Configuration、@EnableAutoConfiguration和@ComponentScan。其中@SpringBootApplication是一个复合注解,包含了@Configuration、@EnableAutoConfiguration和@ComponentScan注解的功能。 4. Spring Boot的实现: Spring Boot的实现基于Spring框架,它使用了大量的注解来简化配置。它还使用了条件注解来根据不同条件自动配置底层框架。另外,Spring Boot还提供了很多starter包,可以让开发人员更加方便快速地搭建应用程序。 5. Spring Boot和Spring MVC的区别: Spring Boot是一个快速构建基于Spring的应用程序的框架,它使用了很多注解来简化配置。而Spring MVC是一个基于MVC架构的Web应用程序框架,它是Spring框架的一部分。Spring Boot可以使用Spring MVC来构建Web应用程序,但是它还可以用于构建其他类型的应用程序。 6. Spring Boot如何跨域请求: Spring Boot可以通过添加一个跨域请求的过滤器来实现跨域请求。具体来说,可以创建一个类实现javax.servlet.Filter接口,然后在类上添加@WebFilter注解,并设置urlPatterns属性来指定需要跨域请求的URL。在过滤器实现的doFilter方法中,设置Access-Control-Allow-Origin、Access-Control-Allow-Methods、Access-Control-Allow-Headers和Access-Control-Max-Age等跨域请求头信息即可。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值