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算法系列>作者. 上一篇文章讲解了<线性表>中的数组.链 ...
随机推荐
- Python Requests库网络爬取全代码
#爬取京东商品全代码 import requestsurl = "http://item.jd.com/2967929.html"try: r = requests.get(url ...
- 【Source Insight 】之marco学习笔记2
现在我们看先看一个 官方地址https://www.sourceinsight.com/download/macro-files/中的 autoexp.em Automatically expands ...
- centos如何安装tomcat
1 通过 SecureCRT 连接到阿里云 CentOS7 服务器; 2 进入到目录 /usr/local/ 中: cd /usr/local/ 3 创建目录 /usr/local/tools,如果有 ...
- HashMap、LinkedHashMap、ConcurrentHashMap、ArrayList、LinkedList的底层实现
HashMap:底层是一个数组+链表实现 LinkedHashMap:底层是Hash表和链表的实现 ConcurrentHashMap:基于双数组和链表的Map接口的同步实现 ArrayList:底层 ...
- air 调用jsfl 执行对应函数,并传参
jsflPath = "WindowSWF/dt_tool_jsfl/" + event.item.fileName+".jsfl"; var element_ ...
- 修改页面中显示出需要修改的数据(包括select选择框复显示)
页面中需要用到某个对象时,在底层代码中赋值,然后页面用java代码进行获取调用 如下截图: select复显示:根据后台方法赋值选择框 ,并设置初始值 按钮及选择框的禁用(五种方法): 方法一: $( ...
- jquery下插入标签以及clone的应用
//内部插入 插入一个儿子 //var $ele = $("<h1></h1>")//创建h1标签 // $ele.html('hello') // $el ...
- 437. Path Sum III
原题: 437. Path Sum III 解题: 思路1就是:以根节点开始遍历找到适合路径,以根节点的左孩子节点开始遍历,然后以根节点的右孩子节点开始遍历,不断循环,也就是以每个节点为起始遍历点 代 ...
- subline 相关
ctrl + ` 输入命令: import urllib.request,os; pf = 'Package Control.sublime-package'; ipp = sublime.insta ...
- Uni2D 入门 -- Atlas转载 http://blog.csdn.net/kakashi8841/article/details/17588095
转载csdnTexture Atlas 我为什么应该使用Texture Atlas? 使用Atlas是一个普遍的好做法,而且它有很多好处.当有某些需要在屏幕渲染的时候,它背后带来的是draw call ...