leetcode 772.Basic Calculator III
这道题就可以结合Basic Calculator中的两种做法了,分别是括号运算和四则运算的,则使用stack作为保持的结果,而使用递归来处理括号内的值的。
class Solution {
public:
int calculate(string s) {
int num=,res=,n=s.size();
stack<int> st;
char op='+';
for(int i=;i<n;i++){
char c=s[i];
if(c>='' && c<=''){
num=num*+c-'';
}else if(c=='('){
int j=i,cnt=;
for(;i<n;i++){
if(s[i]=='(') cnt++;
if(s[i]==')') cnt--;
if(cnt==) break;
}
num=calculate(s.substr(j+,i-j-));
}
if(s[i]=='+' || s[i]=='-' || s[i]=='*' || s[i]=='/' || i==n-){
if(op=='+') st.push(num);
if(op=='-') st.push(-num);
if(op=='*' || op=='/'){
int temp=(op=='*')?(st.top()*num):(st.top()/num);
st.pop();
st.push(temp);
}
op=s[i];
num=;
}
}
while(!st.empty()){
res+=st.top();
st.pop();
}
return res;
}
};
当然这个地方也是可以进行一个变化的,不使用栈,而是考虑用一个变量来保存上一个值的,即栈顶的值,然后继续进行处理的。
class Solution {
public:
int calculate(string s) {
int res=,num=,last=,n=s.size();
char op='+';
for(int i=;i<n;i++){
char c=s[i];
if(c>=''&&c<=''){
num=num*+c-'';
}
else if(c=='('){
int j=i,cnt=;
for(;i<n;i++){
if(s[i]=='(') cnt++;
if(s[i]==')') cnt--;
if(cnt==) break;
}
num=calculate(s.substr(j+,i-j-));
}
if(c=='+' || c=='-' ||c=='*' ||c=='/' ||i==n-){
if(op=='+') last+=num;
if(op=='-') last-=num;
if(op=='*') last*=num;
if(op=='/') last/=num;
if(c=='+'||c=='-'||i==n-){
res+=last;
last=;
}
op=c;
num=;
}
}
return res;
}
};
leetcode 772.Basic Calculator III的更多相关文章
- [LeetCode] 772. Basic Calculator III 基本计算器之三
Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...
- 【leetcode】Basic Calculator III
题目如下: Implement a basic calculator to evaluate a simple expression string. The expression string may ...
- [LeetCode] 227. Basic Calculator II 基本计算器之二
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
- Lintcode: Expression Evaluation (Basic Calculator III)
Given an expression string array, return the final result of this expression Have you met this quest ...
- [LeetCode] 224. Basic Calculator 基本计算器
Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...
- [LeetCode] 227. Basic Calculator II 基本计算器 II
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
- [LeetCode] Basic Calculator III 基本计算器之三
Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...
- LeetCode#227.Basic Calculator II
题目 Implement a basic calculator to evaluate a simple expression string. The expression string contai ...
- Java for LeetCode 227 Basic Calculator II
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
随机推荐
- vs 编译库文件 Qt编译库文件
QT 库能不能用 需要关注是minGW 还是MSVC编译的 Qt MinGW与MSVC对比 转:https://blog.csdn.net/u013185164/article/details/48 ...
- etymon word flower bee apiary forget out~1
1● anth 2● flower 花 1● ap 2● bee 3● apiary 养殖场
- Matlab、R向量与矩阵操作 z
已有 1849 次阅读 2012-8-2 15:15 |系统分类:科研笔记|关键词:矩阵 480 window border center Matlab.R向量与矩阵操作 描 述 Matla ...
- linux下crontab的原理和用法
linux 系统则是由 cron (crond) 这个系统服务来控制的.Linux 系统上面原本就有非常多的计划性工作,因此这个系统服务是默认启动的.另 外, 由于使用者自己也可以设置计划任务,所以, ...
- HTML代码转换为JavaScript字符串
我有时在工作中用到字符串拼接基本上来自于此,链接 http://www.css88.com/tool/html2js/
- jsp自定义标签开发
参考:http://blog.csdn.net/lw001x/article/details/7589302
- bluemix部署(二)构建kubernetes工作环境
本文接上篇.在bluemix中构建kubernetes容器. 1.创建集群 左上角的三横,选容器,然后创建集群. 注意区域,免费版,给个名字,创建集群吧. 继续正在部署,这个可能要15-30分钟,真不 ...
- 外部点击链接,登陆后,直接跳转到该链接(过滤器 + Cookie实现)
一.web.xml (1)指定过滤的Servlet类 (2)配置过滤规则,过滤以.mail结尾的链接 <?xml version="1.0" encoding="U ...
- Cracking The Coding Interview 9.3
//Given a sorted array of n integers that has been rotated an unknown number of times, give an O(log ...
- Cracking The Coding Interview 1.2
//原文: // // Write code to reverse a C-Style String. (C-String means that "abcd" is represe ...