栈 - 20 Valid Parentheses, 150 Evaluate Reverse Polish Notation



注意:!!当字符串的某一字符为 )} ] 时要先判断栈st是否为空,若为空则返回false,否则st.pop()时容易造成指针溢出报错。
class Solution {
public:
bool isValid(string s) {
if(s.empty())
return true;
stack<char> st;
for(auto a : s){
if(a=='(' || a=='['|| a=='{')
st.push(a);
else
{
if(st.empty()) //注意判断栈是否为空,否则st.pop()会导致内存泄漏
return false;
else if(a==')')
{
if(st.top()== '(')
st.pop();
else
return false;
}
else if(a=='}'){
if(st.top() == '{')
st.pop();
else
return false;
}
else{
if(st.top()== '[')
st.pop();
else
return false;
}
}
}
if(st.empty())
return true;
else
return false;
}
};
class Solution {
public:
bool isValid(string s) {
stack<char> st;
for(int i=; i<s.size(); i++){
if(s[i] == '(' || s[i] == '{'|| s[i] == '[')
st.push(s[i]);
else{
//不要忘记判断边界条件
if(st.size() == )
return false;
char c = st.top();
st.pop();
char match;
if(s[i]==')')
match = '(';
else if(s[i]=='}')
match = '{';
else
{
assert(s[i]==']');
match = '[';
}
if(c != match)
return false;
}
}
if(st.size() != )
return false;
return true;
}
};



思路:从前往后遍历数组,遇到数字则压入栈中,遇到符号则把栈顶的两个数字拿出来做运算,把结果再压入栈中,直到遍历完整个数组,栈顶数字就是答案。
class Solution {
public:
int evalRPN(vector<string>& tokens) {
if(tokens.size()==)
return stoi(tokens[]); //string to int
stack<int> st;
for(int i=; i<tokens.size(); i++){
if(tokens[i] != "+" && tokens[i] != "-" && tokens[i] != "*" && tokens[i] != "/")
st.push(stoi(tokens[i]));
else
{
int num1 = st.top();
st.pop();
int num2 = st.top();
st.pop();
if(tokens[i] == "+")
st.push(num1 + num2);
else if(tokens[i] == "-")
st.push(num2 - num1);
else if(tokens[i] == "*")
st.push(num2 * num1);
else{
assert(tokens[i] == "/");
st.push(num2 / num1);
}
}
}
return st.top();
}
};



.. 回退一个目录

思路:使用stringstream来分割字符串,使用字符串t来保存每一段,然后分布处理:当中间是“.”就要直接去掉;多个“/”只保留一个;“..”是回退上一级的意思,即若栈不为空,弹出栈顶元素;最后,将符合要求的字符串压入栈。
class Solution {
public:
string simplifyPath(string path) {
string res, t;
stringstream ss(path);
vector<string> v;
while(getline(ss,t,'/')){ //以 / 来分割ss,获得的字符串赋给t
if(t == "" || t == ".") continue; //跳出本次循环,执行下一个循环
else if(t != ".." )
v.push_back(t);
else if(!v.empty())
v.pop_back();
}
for(string s:v)
res += "/" + s;
return res.empty() ? "/" : res;
}
};
栈 - 20 Valid Parentheses, 150 Evaluate Reverse Polish Notation的更多相关文章
- 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)
[LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...
- 150. Evaluate Reverse Polish Notation - LeetCode
Question 150. Evaluate Reverse Polish Notation Solution 2 1 + 3 * 是((2+1)*3)的后缀(postfix)或逆波兰(reverse ...
- 【LeetCode】150. Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- 【刷题-LeetCode】150 Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- leetcode 150. Evaluate Reverse Polish Notation ------ java
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- 150. Evaluate Reverse Polish Notation逆波兰表达式
[抄题]: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are ...
- [LeetCode] 150. Evaluate Reverse Polish Notation 计算逆波兰表达式
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- 力扣算法题—150. Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, ...
- LeetCode OJ 150. Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
随机推荐
- spring aop自动代理注解配置之一
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- Windows下用Nginx配置遇到的问题
Nginx是一款轻量级的web服务器/反向代理服务器,更详细的释义自己百度了.目前国内像新浪.网易等都在使用它.先说下我的服务器软件环境: 系统:Windows Server + IIS + ngin ...
- 【原创】请不要对Boost Format使用Byte作为参数
曾几何时我们可以肆无忌惮的对sprintf传入BYTE等类型作为参数,只要你指定的为%D即可打印出对应的数字 但是boost format不可以,当你发生类型截断,错误,异常,请尽快查看你传入的类型是 ...
- 【原创测试】MongoDB千万级插入数据测试(MMO在线游戏应用场合)
一.筹备 我们要做一次千万级的MONGODB测试,操作系统选用CentOS 5.5 64位版,基本模拟实际的使用环境,采用单机集群模型(测试单机多CPU情况下的实际效果). 测试基准数据: 服务器配置 ...
- 黑盒测试实践--Day7 12.1
黑盒测试实践--Day7 12.1 今天完成任务情况: 录制小组作业中的自动化测试工具实践视频 汇总大家提交的各种作业模块,打包完成小组共同作业 小组成员完成个人情况说明后在截止时间前分别提交作业 小 ...
- Regist&Login
关于注册页面和登录页面的业务流程 form表单中确定action提交地址 method 确定提交的方法--->写出相对应的Servlet,假如接受的数据不多 ,那么用 String userna ...
- Laravel 的 make:auth Artisan 命令到底生成了哪些文件?
众所周知,在 Laravel 中执行 $ php artisan make:auth $ php artisan migrate 命令后,我们就能拥有一个完整的登录.注册认证系统,这为开发带来极大的便 ...
- C++ 中 dynamic_cast 浅析
简述:dynamic_cast 操作符,将基类的指针或引用安全的转换为派生类的指针或引用.主要讲解,dynamic_cast操作符的原理.使用方式.编译器设置.返回值等相关知识. dynamic_ca ...
- 编写高质量代码改善C#程序的157个建议——建议30:使用LINQ取代集合中的比较器和迭代器
建议30:使用LINQ取代集合中的比较器和迭代器 LINQ提供了类似于SQL的语法来实现遍历.筛选与投影集合的功能. static void Main(string[] args) { List< ...
- POJ3295 Tautology(栈+枚举)
Description WFF 'N PROOF is a logic game played with dice. Each die has six faces representing some ...