Solution 29: 合法的pop序列
2015-07-10 10:45
169 查看
问题描述
输入两个整数序列,其中一个为入栈序列,另一个为出栈序列。判断出栈序列是否是合法的。
解决思路
使用一个辅助栈,用最直观的方式。
程序
输入两个整数序列,其中一个为入栈序列,另一个为出栈序列。判断出栈序列是否是合法的。
解决思路
使用一个辅助栈,用最直观的方式。
程序
public class ValidPopSequence { public boolean isValidPopSeq(int[] push, int[] pop) { if (push == null && pop == null) { return true; } int i = 0, j = 0; Stack<Integer> s = new Stack<Integer>(); while (j < pop.length) { while (i < push.length) { if (push[i] != pop[j]) { s.push(push[i]); ++i; } else { // same, no push, pop next ++i; ++j; break; } } if (i == push.length) { // end push // check top of stack with pop if (s.isEmpty() || s.peek() != pop[j]) { return false; } s.pop(); ++j; } } return true; } }
相关文章推荐
- linux和windows的文件夹共享和互相挂载
- 一种定位内存泄露的方法(Linux)
- linux内存
- 国外优秀软件测试网站介绍及测试资料
- Centos 安装R 集成 Hadoop、RHive 配置安装手册
- Tomcat7修改根路径应用
- Centos安装ImageMagick 与 imagick for php详解
- 简单的shell scripts例子
- Win7下用IIS发布网站
- Linux下内存统计和内存泄露类问题的定位方法
- opencv学习笔记一(配置)
- linux系统怎么重启网卡?linux重启网卡的三种教程
- Linux下halt,poweroff,reboot,shutdown命令的区别
- Linux 常用命令
- Linux FTP服务器-VSFTPD虚拟用户配置
- linux(centos)下通过yum安装mysql5.6
- tomcat outofmemory
- 7月7日全球域名商(国际域名)解析新增保有量TOP20
- MACOS,LINUX,IOS上可用的毫秒级精度时间获取
- oauth2.0在监控宝项目中的应用一例