博客
关于我
栈和队列的经典例题(括号匹配,用栈实现队列,用队列实现栈,设计循环队列)
阅读量:631 次
发布时间:2019-03-14

本文共 4436 字,大约阅读时间需要 14 分钟。

栈和队列的基础接口实现

一、括号匹配

给定一个只包括 '(', ')', '{', '}', '[', ']' 的字符串,判断字符串是否有效。

#include 
#include
using namespace std;class Solution {public: bool isValid(string s) { stack
stack_ch; int size = s.size(); for (int i = 0; i < size; ++i) { char ch = s[i]; switch (ch) { case '(': case '{': case '[': { stack_ch.push(ch); break; } case ')': case '}': case ']': { if (stack_ch.empty()) { return false; } char left = stack_ch.top(); if (!((left == '(' && ch == ')') || (left == '[' && ch == ']') || (left == '{' && ch == '}'))) { return false; } stack_ch.pop(); break; } default: { break; } } } return stack_ch.empty(); }};

二、用队列实现栈

#include 
class MyStack {public: queue
q; MyStack() { } void push(int x) { q.push(x); } int pop() { if (q.empty()) { return -1; // 可按实际需求处理空栈情况 } int size = q.size() - 1; for (int i = 0; i < size; ++i) { int data = q.front(); q.pop(); q.push(data); } int d = q.front(); q.pop(); return d; } int top() { if (q.empty()) { return -1; } int size = q.size() - 1; for (int i = 0; i < size; ++i) { int data = q.front(); q.pop(); q.push(data); } int d = q.front(); q.pop(); q.push(d); return d; } bool empty() { return q.empty(); }};

三、用栈实现队列

#include 
class MyQueue {public: stack
left; stack
right; MyQueue() { } void push(int x) { right.push(x); } int pop() { if (left.empty()) { int size = right.size(); for (int i = 0; i < size; ++i) { int d = right.top(); right.pop(); left.push(d); } } if (left.empty()) { return -1; // 可按实际需求处理空队列情况 } int v = left.top(); left.pop(); return v; } int peek() { if (left.empty()) { int size = right.size(); for (int i = 0; i < size; ++i) { int d = right.top(); right.pop(); left.push(d); } } if (left.empty()) { return -1; // 可按实际需求处理空队列情况 } return left.top(); } bool empty() { return left.empty() && right.empty(); }};

四、设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈

#include 
class MinStack {public: stack
normal; stack
min; MinStack() { } void push(int x) { normal.push(x); if (min.empty() || x <= min.top()) { min.push(x); } else { min.push(min.top()); } } void pop() { normal.pop(); min.pop(); } int top() { return normal.top(); } int getMin() { return min.top(); }};

五、设计一个循环队列

#include 
class MyCircularQueue {public: int* array; int size; int capacity; int front; int rear; MyCircularQueue(int k) { array = new int[k]; capacity = k; front = 0; rear = 0; size = 0; } bool enQueue(int value) { if (size == capacity) { return false; } array[rear] = value; rear = (rear + 1) % capacity; size++; return true; } bool deQueue() { if (size == 0) { return false; } front = (front + 1) % capacity; size--; return true; } int Front() { if (size == 0) { return -1; } return array[front]; } int Rear() { if (size == 0) { return -1; } int index = (rear + capacity - 1) % capacity; return array[index]; } bool isEmpty() { return size == 0; } bool isFull() { return size == capacity; }};

转载地址:http://cchoz.baihongyu.com/

你可能感兴趣的文章
mysql5.7免费下载地址
查看>>
mysql5.7命令总结
查看>>
mysql5.7安装
查看>>
mysql5.7性能调优my.ini
查看>>
MySQL5.7新增Performance Schema表
查看>>
Mysql5.7深入学习 1.MySQL 5.7 中的新增功能
查看>>
Webpack 之 basic chunk graph
查看>>
Mysql5.7版本单机版my.cnf配置文件
查看>>
mysql5.7的安装和Navicat的安装
查看>>
mysql5.7示例数据库_Linux MySQL5.7多实例数据库配置
查看>>
Mysql8 数据库安装及主从配置 | Spring Cloud 2
查看>>
mysql8 配置文件配置group 问题 sql语句group不能使用报错解决 mysql8.X版本的my.cnf配置文件 my.cnf文件 能够使用的my.cnf配置文件
查看>>
MySQL8.0.29启动报错Different lower_case_table_names settings for server (‘0‘) and data dictionary (‘1‘)
查看>>
MYSQL8.0以上忘记root密码
查看>>
Mysql8.0以上重置初始密码的方法
查看>>
mysql8.0新特性-自增变量的持久化
查看>>
Mysql8.0注意url变更写法
查看>>
Mysql8.0的特性
查看>>
MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
查看>>
MySQL8修改密码的方法
查看>>