leetcode 栈和队列类型题
1,Valid Parentheses
bool isVaild1(string& s) { // 直接列举,不易扩展
stack<char> stk;
for (int i = ; i < s.length(); ++i) {
if (stk.empty())
stk.push(s[i]);
else {
char top = stk.top();
if (top == '(' && s[i] == ')' || top == '{' && s[i] == '}' || top == '[' && s[i] == ']')
stk.pop();
}
}
if (stk.empty())
return true;
else
return false;
}
bool isValid2(string& s) {
string left = "([{";
string right = ")]}";
stack<char> stk;
for (auto c = s.begin(); c != s.end(); ++c) {
if (left.find(*c) != string::npos)
stk.push(*c);
else {
if (stk.empty() || stk.top() != left[right.find(*c)])
return false;
else
stk.pop();
}
}
return stk.empty();
}
isVaild
2,Longest Valid Parentheses
int longestValidParentheses(const string& s) {
int len = ;
stack<char> stk;
for (size_t i = ; i < s.size(); ++i) {
if (stk.empty())
stk.push(s[i]);
else {
if (stk.top() == '(' && s[i] == ')') {
stk.pop();
++len;
}
else
stk.push(s[i]);
}
}
return * len;
}
longestValidParentheses
3,Largest Rectangle in Histogram
int longestRectangleArea1(vector<int>& heights) { // 暴力求解
if (heights.size() == ) return ;
int result = ;
for (int i = ; i < heights.size(); ++i) {
int minHeight = heights[i];
if (i == heights.size() - || heights[i]>heights[i + ]) { // 简单优化
for (int j = i; j >= ; --j) {
minHeight = min(minHeight,heights[j]);
result = max((i - j + )*minHeight, result);
}
}
}
return result;
}
int longestRectangleArea2(vector<int>& heights) { // 用 stack实现,未看懂
stack<int> s;
heights.push_back();
int result = ;
for (int i = ; i < heights.size();) {
if (s.empty() || heights[i] > heights[s.top()])
s.push(i++);
else {
int temp = s.top();
s.pop();
result = max(result, heights[temp] * (s.empty() ? i : i - s.top() - ));
}
}
return result;
}
longestRectangleArea
4,Evaluate Reverse Polish Notation
int evalRPN(vector<string>& tokens) {
stack<int> stk;
string options = "+-*/";
for (int i = ; i < tokens.size(); ++i) {
if (options.find(tokens[i]) == string::npos) // 不是运算符
stk.push(atoi(tokens[i].c_str()));
else {
int num1 = stk.top();
stk.pop();
int num2 = stk.top();
stk.pop();
if (tokens[i] == "+") stk.push(num1 + num2);
else if (tokens[i] == "-") stk.push(num1 - num2);
else if (tokens[i] == "*") stk.push(num1 * num2);
else stk.push(num1 / num2);
}
}
return stk.top();
}
evalRPN
以上题目来源于:https://github.com/soulmachine/leetcode(leetcode-cpp.pdf)
leetcode 栈和队列类型题的更多相关文章
- DS博客作业03——栈和队列
1.本周学习总结 谈谈你对栈和队列结构的认识及学习体会. 栈和队列的本质就是线性表.所以,相应的栈跟队列都有两种存储结构:顺序存储结构.链式存储结构. 栈的特点是后进先出,根据栈时进时出的规则,出栈的 ...
- LeetCode刷题 --杂篇 --数组,链表,栈,队列
武汉加油,中国加油.希望疫情早日结束. 由于疫情,二狗寒假在家不能到处乱逛,索性就在家里系统的刷一下算法的内容,一段时间下来倒也有些小小的收获.只是一来家中的小破笔记本写起博客来实在不是很顺手,二来家 ...
- 【leetcode 简单】 第六十六题 用栈实现队列
使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从队列首部移除元素. peek() -- 返回队列首部的元素. empty() -- 返回队列是否为空. ...
- LeetCode 232题用栈实现队列(Implement Queue using Stacks) Java语言求解
题目链接 https://leetcode-cn.com/problems/implement-queue-using-stacks/ 题目描述 使用栈实现队列的下列操作: push(x) -- 将一 ...
- C#LeetCode刷题之#232-用栈实现队列(Implement Queue using Stacks)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4108 访问. 使用栈实现队列的下列操作: push(x) -- ...
- leetcode刷题记录——栈和队列
题目 232.用栈实现队列 class MyQueue { private Stack<Integer> in = new Stack<>(); private Stack&l ...
- Leetcode栈&队列
Leetcode栈&队列 232.用栈实现队列 题干: 思路: 栈是FILO,队列是FIFO,所以如果要用栈实现队列,目的就是要栈实现一个FIFO的特性. 具体实现方法可以理解为,准备两个栈, ...
- c++刷题(3/100)数独,栈和队列
stack的基本操作 • s.size():返回栈中的元素数量 • s.empty():判断栈是否为空,返回true或false • s.push(元素):返回对栈顶部“元素”的可变(可修改)引用 • ...
- ACM金牌选手讲解LeetCode算法《栈和队列的高级应用》
大家好,我是编程熊,双非逆袭选手,字节跳动.旷视科技前员工,ACM金牌,保研985,<ACM金牌选手讲解LeetCode算法系列>作者. 上一篇文章讲解了<线性表>中的数组.链 ...
随机推荐
- elk之[logstash-input-file]插件使用详解
https://www.cnblogs.com/xing901022/p/4805586.html http://www.cnblogs.com/xing901022/p/4802822.html ...
- 05.linux目录结构
bin 存放二进制可执行文件(ls,cat,mkdir等) boot 存放用于系统引导时使用的各种文件 dev 用于存放设备文件 etc 存放系统配置文件 home 存放所有 ...
- SVM标记学习
# -*- coding: utf-8 -*- """ Created on Mon Oct 1 09:32:37 2018 @author: ""& ...
- Linux查看当前系统的发行版信息
lsb_release命令用来查看当前系统的发行版信息(prints certain LSB (Linux Standard Base) and Distribution information.). ...
- WDA-3-ALV查询
主要是梳理下WebDynpro For ABAP开发过程: 1.创建WebDynpro组件 2.创建WebDynpro应用 1.创建WebDynpro组件 1.1创建 路径:选择Package--&g ...
- Python集合的基本操作
#-*coding:utf-8 -* list =set([2,3,4]) list2 =set([5,3,7]) #交集 #print (list.intersection(list2)) #并集 ...
- datetime is not json serializable
python, datetime is not json serializable import datetime def json_serial(obj): """JS ...
- cxgrid中回车键光标移到下列
OptionsBehavior.GoToNextCellOnEnter:=True; 更完善的回车 可以在焦点到了最后一列再回车时有下一行则移到下一行的第一列,没有下一行则新增记录并移到第一列 pro ...
- JSON数据的解析和生成(Swift)
Codable public typealias Codable = Decodable & Encodable public protocol Decodable {} public pro ...
- Python开发环境搭建指导
本文主要介绍Python开发环境的搭建.主要包括如下几部分内容: (1)Python软件的安装.注意版本的选择和安装过程中选项的勾选. (2)pip工具环境变量.镜像源的配置使用和常用镜像源介绍.pi ...