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算法系列>作者. 上一篇文章讲解了<线性表>中的数组.链 ...
随机推荐
- leetcode149
/* * A line is determined by two factors,say y=ax+b * * If two points(x1,y1) (x2,y2) are on the same ...
- Promise笔记
参考:阮一峰es6(http://es6.ruanyifeng.com/#docs/promise) 时间:2018-07-03 类型:个人笔记 解决的问题:异步编程的一种解决方案. 定义:Promi ...
- 浅析USB HID ReportDesc (HID报告描述符)
在USB中,USB Host是通过各种描述符来识别识别设备的,一般在设备枚举的过程将会获取有设备描述符/配置描述符/接口描述符/端点描述符/字符串描述符等 现在我们来介绍一下HID ReportDes ...
- charles抓包的安装,使用说明以及常见问题解决(windows)
charles抓包的安装,使用说明以及常见问题解决(windows) https://blog.csdn.net/zhangxiang_1102/article/details/77855548
- 机器学习进阶-图像特征sift-SIFT特征点 1.cv2.xfeatures2d.SIFT_create(实例化sift) 2. sift.detect(找出关键点) 3.cv2.drawKeypoints(画出关键点) 4.sift.compute(根据关键点计算sift向量)
1. sift = cv2.xfeatures2d.SIFT_create() 实例化 参数说明:sift为实例化的sift函数 2. kp = sift.detect(gray, None) 找出 ...
- mui init 出现无法引入子页面问题
1. 检查项目中是否重复出现了 mui.init() 函数; mui.init({ subpages: [{ styles: { // top: "44px", top: &quo ...
- hadoop-1
结合其他文章 http://weixiaolu.iteye.com/blog/1504898 https://www.cnblogs.com/dycg/p/3934394.html https://b ...
- python中函数基础
函数 什么是函数? 函数分为内置函数和自定义函数 定义:在程序中具备某一功能的工具.在使用之前需准备该工具(函数的定义),遇到应用场景拿来就用(后引用). 为什么要用函数? 1.代码冗余 程序组织结构 ...
- C# 2018.9.17
C#的优点:1,不会有运行时崩溃,解决了C++的痛点一,难预防,难查错2,使用文件不需要包含进来,只需要using namespace即可,解决了C++的痛点二,包含复杂,路径复杂,编译复杂3,编译速 ...
- Stephen Hawking Taught Us a Lot About How to Live
勇气.好奇心.幽默感,那些霍金教给我们的事Stephen Hawking Taught Us a Lot About How to LiveStephen Hawking, the English c ...