注意:!!当字符串的某一字符为 )}  ] 时要先判断栈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的更多相关文章

  1. 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)

    [LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...

  2. 150. Evaluate Reverse Polish Notation - LeetCode

    Question 150. Evaluate Reverse Polish Notation Solution 2 1 + 3 * 是((2+1)*3)的后缀(postfix)或逆波兰(reverse ...

  3. 【LeetCode】150. Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

  4. 【刷题-LeetCode】150 Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

  5. leetcode 150. Evaluate Reverse Polish Notation ------ java

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  6. 150. Evaluate Reverse Polish Notation逆波兰表达式

    [抄题]: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are ...

  7. [LeetCode] 150. Evaluate Reverse Polish Notation 计算逆波兰表达式

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  8. 力扣算法题—150. Evaluate Reverse Polish Notation

      Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +,  ...

  9. LeetCode OJ 150. Evaluate Reverse Polish Notation

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

随机推荐

  1. mybatis 框架 的简单使用

    # Global logging configuration #在开发环境下日志级别要设置成DEBUG,生产环境设置成info或error log4j.rootLogger=DEBUG, stdout ...

  2. 我使用的网址--Hadoop

    1.Hadoop 官网下载:http://hadoop.apache.org/releases.html 各版本网址:http://mirror.bit.edu.cn/apache/hadoop/co ...

  3. POJ - 2109 Power of Cryptography(高精度log+二分)

    Current work in cryptography involves (among other things) large prime numbers and computing powers ...

  4. SNMP协议学习笔记

    什么是SNMP协议? SNMP协议是以UDP为基础的应用层协议,全称为 简单网络管理协议,用于网络管理系统与被管设备(路由器,交换机,服务器等设备)进行通信. 应用场景 随着网络设备的增多,需要单独的 ...

  5. Newtonsoft.Json.Linq

    var json = "{\"name\":\"ok1\",\"sex\":\"man\"}"; / ...

  6. linux版本信息以及x86与x86_64的区别

    一 x86.x86_64.AMD64 x86是指intel的开发的一种32位指令集,从386开始时代开始的,一直沿用至今,是一种cisc指令集,所有intel早期的cpu,amd早期的cpu都支持这种 ...

  7. Server Sql 多表查询、子查询和分页

    一.多表查询:根据特定的连接条件从不同的表中获取所需的数据 多表查询语法: SELECT table1.column, table2.column FROM table1, table2 WHERE ...

  8. go channel缓冲区的大小

    go channel缓冲区的大小 len也可以作用于channel,代表现在channel缓冲区中还有多少数据没有读取.示例如下 c:=make(chan int,20) fmt.Println(&q ...

  9. SQL语句也可以重构优化

    真的,不管是程序中的代码可以重构优化,在SQL Server的语句,也是可以的.下面举个例子,在存储过程中,所传入的数据参数不能为空,另外在对数据表进行更新时,所更新的字段如果是空的话,就更新,如果传 ...

  10. css css预处理器

    CSS预处理器(css preprocessor) 1.less: 2.sass: 3.scss: 4.stylus 参考: http://hao.jser.com/archive/2507/ htt ...