题目:

Implement a basic calculator to evaluate a simple expression string.

The expression string contains only non-negative integers, +-*/ operators
and empty spaces . The integer division should truncate toward zero.

You may assume that the given expression is always valid.

Some examples:

"3+2*2" = 7
" 3/2 " = 1
" 3+5 / 2 " = 5

Note: Do not use the eval built-in
library function.

Credits:

Special thanks to @ts for adding this problem and creating all test cases.

代码:

class Solution {
public:
int calculate(string s) {
stack<int> st;
int result = 0;
int i = 0;
while(i<s.size())
{
int sum = 0;
if(i<s.size()&&s.at(i)>='0'&&s.at(i)<='9')
{
while(i<s.size()&&s.at(i)>='0'&&s.at(i)<='9')
{
sum = sum * 10 + s.at(i)-'0';
i++;
}
st.push(sum);
}
else if(i<s.size()&&s.at(i)=='+')
{
st.push((int)s.at(i));
i++;
}
else if(i<s.size()&&s.at(i)=='-')
{
st.push((int)s.at(i));
i++; }
else if(i<s.size()&&s.at(i)=='*')
{
int a = st.top();
st.pop();
int sum1 = 0;
i++;
while(s.at(i)==' ')
{
i++;
}
if(i<s.size()&&s.at(i)>='0'&&s.at(i)<='9')
{
while(i<s.size()&&s.at(i)>='0'&&s.at(i)<='9')
{
sum1 = sum1 * 10 + s.at(i)-'0';
i++;
}
a*=sum1;
st.push(a);
}
}
else if(i<s.size()&&s.at(i)=='/')
{ int a = st.top();
st.pop();
int sum1 = 0;
i++;
while(s.at(i)==' ')
{
i++;
}
while(i<s.size()&&s.at(i)>='0'&&s.at(i)<='9')
{
sum1 = sum1 * 10 + s.at(i)-'0';
i++;
}
a = (int)(a/sum1);
st.push(a);
}
else
{
i++;
}
}
stack<int> st1;
while(!st.empty())
{
st1.push(st.top());
st.pop();
}
while(!st1.empty())
{
int a = st1.top();
st1.pop();
if(st1.empty())
return a;
else
{
if((char)(st1.top())=='+')
{
st1.pop();
int temp = st1.top()+a;
st1.pop();
st1.push(temp);
}
else if((char)(st1.top())=='-')
{
st1.pop();
int temp = a-st1.top();
st1.pop();
st1.push(temp);
}
}
}
return st1.top();
}
};

方法比较繁琐。。。就这样了。

leetcode_Basic Calculator II的更多相关文章

  1. Basic Calculator,Basic Calculator II

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

  2. leetcode 224. Basic Calculator 、227. Basic Calculator II

    这种题都要设置一个符号位的变量 224. Basic Calculator 设置数值和符号两个变量,遇到左括号将数值和符号加进栈中 class Solution { public: int calcu ...

  3. 【LeetCode】227. Basic Calculator II

    Basic Calculator II Implement a basic calculator to evaluate a simple expression string. The express ...

  4. [LeetCode] Basic Calculator & Basic Calculator II

    Basic Calculator Implement a basic calculator to evaluate a simple expression string. The expression ...

  5. LeetCode OJ Basic Calculator II

    Basic Calculator II 题目 思路 和这个一样:Basic Calculator I 代码 class ExpressionTransformation { public: strin ...

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

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

  7. 【LeetCode】227. Basic Calculator II 解题报告(Python)

    [LeetCode]227. Basic Calculator II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...

  8. [LeetCode] Basic Calculator II 基本计算器之二

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

  9. Basic Calculator II

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

随机推荐

  1. MacBook Air 2014 安装win7

    1.准备一个4G以上容量USB3.0 U盘.制作一个带USB3.0驱动的win7 2.将制作好的win7iso镜像文件复制到macbook上,插上U盘,运行Boot Camp助理: 3.选择默认勾选项 ...

  2. Ubuntu设置中文-以及-安装拼音输入法

    2017-11-12更新 安装搜狗拼音: http://blog.csdn.net/iamplane/article/details/70447517 ------------------------ ...

  3. thinkphp 设置 支持模块多组

    正常的访问地址是:localhost/项目名/index.php/Admin/Index/index 模块分组之后呢:localhost/项目名/index.php/Admin/System/Inde ...

  4. [android] android 获取网络连接信息

    效果图:  工具类 /** * 获取网络连接信息 * * 根据NetworkInfo可以知道有很多的连接方式和信息 * * ① 当没有任何可用网络的时候,networkinfo为null 判断netw ...

  5. Spring Boot注解(annotation)列表

    (1)@SpringBootApplication 申明让spring boot自动给程序进行必要的配置,这个配置等同于: @Configuration ,@EnableAutoConfigurati ...

  6. oracle和SQLserver数据库中select into 的区别

    在Oracle中是这样的 在SQLserver中是这样的

  7. MySQL单列索引和组合索引的选择效率与explain分析

    一.先阐述下单列索引和组合索引的概念: 单列索引:即一个索引只包含单个列,一个表可以有多个单列索引,但这不是组合索引. 组合索引:即一个索包含多个列. 如果我们的查询where条件只有一个,我们完全可 ...

  8. ip地址查询系统和CMD查询的结果不一样

    由于cmd输入 ipconfig查看的IP是局域网内网IP,而用ip地址查看器查看是公网上网的ip地址.所以不一样. 查询内网ip: windows系统: 开始--运行--cmd,命令行输入: ipc ...

  9. [ASK] brew install nginx

    .......... .......... Error: Permission denied - /usr/local/etc/openssl .......... .......... Cannot ...

  10. python2迁移python3的问题

    ▌使用 pathlib 模块来更好地处理路径 pathlib 是 Python 3默认的用于处理数据路径的模块,它能够帮助我们避免使用大量的 os.path.joins语句: from pathlib ...