栈 - 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 +, -, ...
随机推荐
- 217. Contains Duplicate数组重复元素 123
[抄题]: Given an array of integers, find if the array contains any duplicates. Your function should re ...
- 2014年Linux 和开源技术回顾盘点
ZDNet科技观察家StevenJ.Vaughan-Nichols在年终发表了对Linux和开源技术这一年跌宕起伏的总结,细数这一年中的惊喜和不堪. 2014Linux之殇 “心脏出血(Heartbl ...
- Ubuntu 复制 拷贝和自适应屏幕
ubuntu 16.04安装vmtools实测无效!!!!!!11 1.解决VMware workstation与主机的粘贴.复制.文件拖拽问题. 2.解决VMware workstations中Ub ...
- Linux下DNS配置
一.本机DNS配置 参考:http://blog.sina.com.cn/s/blog_68d6e9550100k3b7.html 二.DNS服务器搭建 http://toutiao.com/i631 ...
- C# winform中Setting.settings 相关知识点
1.在Settings.settings文件中定义配置字段.包含字段名.类型.范围.值四部分的属性. 字段名.类型和值类似编程中字段的定义一样使用,不再过多的解释.重点讲一下”范围“字段的含义与区别. ...
- 软工作业WordCount
github项目传送门:https://github.com/zzh010/My-wc 一.WC 项目要求 wc.exe 是一个常见的工具,它能统计文本文件的字符数.单词数和行数.这个项目要求写一个命 ...
- C# - dynamic 特性
dynamic是FrameWork4.0的新特性.dynamic的出现让C#具有了弱语言类型的特性.编译器在编译的时候不再对类型进行检查,编译期默认dynamic对象支持你想要的任何特性. 比如,即使 ...
- windows phone之依赖属性(DependencyProperty)
Windows Presentation Foundation (WPF) 提供了一组服务,这些服务可用于扩展公共语言运行时 (CLR) 属性的功能,这些服务通常统称为 WPF 属性系统.由 WPF ...
- SpringMVC+Hibernate 项目开发之二 (STS整合Maven)
为什么用STS不用Eclipse,主要是Eclipse集成Maven把我整疯了,最后估计原因除在网速上了. 其实用了STS以后发现还真比Eclipse好用点. STS本身集成有Maven的,但是默认的 ...
- kubernetes dashboard 安装
环境:CentOS Linux release 7.3.1611 (Core)IP:192.168.0.103 [1]组件安装yum install device-mapperyum install ...