这道题是只有四则运算但是没有括号的,因此可以利用stack来存储的,并且每次使得存储的值与符号有对应的关系,最后在栈中只剩下可以利用加法进行处理的数的,注意在i=n-1的时候到达最后的部分也是需要把数字压入栈中的而不能被忽略掉。

class Solution {
public:
int calculate(string s) {
int res=,num=,n=s.size();
stack<int> st;
char op='+';
for(int i=;i<n;i++){
if(s[i]>=''){
num=num*+s[i]-'';
}
if((s[i]<''&&s[i]!=' ') || i==n-){
if(op=='+') st.push(num);
if(op=='-') st.push(-num);
if(op=='*'){
int temp=st.top()*num;
//cout<<temp<<endl;
st.pop();
st.push(temp);
}
if(op=='/'){
int temp=st.top()/num;
st.pop();
st.push(temp);
}
op=s[i];
num=;
//cout<<st.top()<<endl;
}
}
while(!st.empty()){
res+=st.top();
st.pop();
}
return res;
}
};

leetcode227. Basic CalculatorII的更多相关文章

  1. LeetCode227:Basic Calculator II

    Implement a basic calculator to evaluate a simple expression string. The expression string contains ...

  2. Basic Calculator,Basic Calculator II

    一.Basic Calculator Total Accepted: 18480 Total Submissions: 94750 Difficulty: Medium Implement a bas ...

  3. LeetCode 227. 基本计算器 II(Basic Calculator II)

    227. 基本计算器 II 227. Basic Calculator II 题目描述 实现一个基本的计算器来计算一个简单的字符串表达式的值. 字符串表达式仅包含非负整数,+,-,*,/ 四种运算符和 ...

  4. [Swift]LeetCode227. 基本计算器 II | Basic Calculator II

    Implement a basic calculator to evaluate a simple expression string. The expression string contains ...

  5. Atitit HTTP 认证机制基本验证 (Basic Authentication) 和摘要验证 (Digest Authentication)attilax总结

    Atitit HTTP认证机制基本验证 (Basic Authentication) 和摘要验证 (Digest Authentication)attilax总结 1.1. 最广泛使用的是基本验证 ( ...

  6. Basic Tutorials of Redis(9) -First Edition RedisHelper

    After learning the basic opreation of Redis,we should take some time to summarize the usage. And I w ...

  7. Basic Tutorials of Redis(8) -Transaction

    Data play an important part in our project,how can we ensure correctness of the data and prevent the ...

  8. Basic Tutorials of Redis(7) -Publish and Subscribe

    This post is mainly about the publishment and subscription in Redis.I think you may subscribe some o ...

  9. Basic Tutorials of Redis(6) - List

    Redis's List is different from C#'s List,but similar with C#'s LinkedList.Sometimes I confuse with t ...

随机推荐

  1. stealwatch里的安全功能——ETA结果会显示加密套件以及key长度,还有流量大小(例如41MB)

    以后可以考虑的方向,在stealwatch里包含: ad Injector click fraud cryptocurrency miner exploit kit malicious adverti ...

  2. summary_16th Nov, 2018

    一. 编程语言的分类: a. 机器语言:直接使用二进制指令去编写程序,必须考虑硬件细节 b:汇编语言:用英文标签取代二进制指令去编写程序,必须考虑硬件细节 c:高级语言:用人类能理解的方式编写程序,通 ...

  3. Astah Professional安装

    asish安装 1● 文件下载   2● 安装图解 3● 破解 replace 4● 测试 success  

  4. 动态规划-最长单调递增子序列(dp)

    最长单调递增子序列 解题思想:动态规划 1.解法1(n2) 状态:d[i] = 长度为i+1的递增子序列的长度 状态转移方程:dp[i] = max(dp[j]+1, dp[i]); 分析:最开始把d ...

  5. 技术宅学习Linux系统还是很有前途的

    老实说,我之所以入了Linux的坑,纯粹只是为了追我现在的男朋友,也就是技术宅.如果不是为了追我男朋友的话,我估计我这辈子都不会去接触linux.好吧,今天写一写过往事情,也是为了怀念当初追男友的一些 ...

  6. bzoj2055

    题解: 似乎是放在费用流里的 然而是有上下界的网络流QAQ 代码: #include<bits/stdc++.h> using namespace std; ; int n,m,x,min ...

  7. 【阅读笔记】《C程序员 从校园到职场》第三章 程序的样式(大括号)

    参考: https://blog.csdn.net/zhouzhaoxiong1227/article/details/22820533 一..初始化数组变量 在实际的软件开发项目中,变量在使用前应初 ...

  8. [Leetcode 44]通配符匹配Wildcard Matching

    [题目] 匹配通配符*,?,DP动态规划,重点是*的两种情况 想象成两个S.P长度的字符串,P匹配S. S中不会出现通配符. [条件] (1)P=null,S=null,TRUE (2)P=null, ...

  9. python socket 网络编程selector用法 (实用)

    Server端: import socketimport selectors class Server(object):def init(self,sel,sock):self.sel = selse ...

  10. 某些material英文翻译

    chrome  铬,铬合金, 镀铬 matte  无光泽的,不光滑的 Decal   陶瓷的 duo color  双色 livery  (这个没找到什么吊意思,我看像是一个类似打logo的材质)