堆栈 (LIFO)
来源:互联网 发布:mac mail gmail 编辑:程序博客网 时间:2024/05/02 23:46
数组实现:
#include <stdio.h>#define N 1000int main(){ int i, n, target[N]; while(scanf("%d", &n) == 1) { int stack[N], top = -1; for(i = 0; i < n; i++) scanf("%d", &target[i]); int A = 1, B = 0; int ok = 1; while(B < n) { if(A == target[B]) { A++; B++; } else if(top >= 0 && stack[top] == target[B]) { top--; B++; } else if(A <= n) { stack[++top] = A++; } else { ok = 0; break; } } printf("%s\n", ok ? "Yes" : "No"); } return 0;}
STL实现:
#include <cstdio>#include <stack>#define N 1000using namespace std;int main(){ int i, n, target[N]; while(scanf("%d", &n) == 1) { stack<int> s; for(i = 0; i < n; i++) scanf("%d", &target[i]); int A = 1, B = 0, ok = 1; while(B < n) { if(A == target[B]) { A++; B++; } else if(!s.empty() && s.top() == target[B]) { s.pop(); B++; } else if(A <= n) { s.push(A++);} else { ok = 0; break; } } printf("%s\n", ok ? "Yes" : "No"); } return 0;}
0 0
- 堆栈 (LIFO)
- 下压堆栈的链表实现(LIFO)
- 堆栈的实现(LIFO、链表)
- VDHL 内嵌BRAM设计LIFO堆栈
- python实现堆栈 后进先出 LIFO
- 下压(LIFO)栈,ResizingArrayStack
- 栈(LIFO:后进先出)
- 算法1.1 下压(LIFO)栈 (能够动态调整数组大小的实现)
- 栈(Stack)——后进先出(LIFO)的数据结构(Data Structures)
- Get Minimal value in constant time from a LIFO(and FIFO) stack
- 算法1.1 下压栈(LIFO)能动态调整数组的大小
- 下压(LIFO)栈(能够动态调整数组大小的实现)
- 向消息队列发送一个消息(后进先出LIFO),OSQPostFront()
- 进销存管理(FIFO,LIFO)
- _onexit注册函数,LIFO
- 下压(LIFO栈)_API
- Python--FIFO,LIFO,ListC,Modules
- java堆栈 (转)
- Android TextView属性详解
- unity3D java c#交互
- 好消息: 《微信商城开发实战》 已经由电子工业出版社出版发行啦
- 黑马程序员——TreeSet集合
- Valid Parentheses
- 堆栈 (LIFO)
- 【练习笔记】剑指offer-输出n位数全排列
- 设计模式(代理模式--动态代理)
- 欢迎使用CSDN-markdown编辑器
- JDBC编程简明教程
- Invert Binary Tree
- linux shell(一)
- 继承
- stl源码学习