题目:

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. Idea上配置btm

    1.  先在eclipse中配置好项目,再讲配置好的项目导入到idea中

  2. Android——Activity中的六个主要函数

    Android Activity中的六个主要函数 Android中一个Activity一般都需要实现六个函数: onCreate(), onStart(), onResume(),onPause(), ...

  3. PHP——数组2(数组函数,二维数组,正则表达式)

    <body> <?php //数组函数 $arr=array(1,2,3,4,5,6); print_r($arr); echo "<br />"; ...

  4. ubuntu boot空间不足的解决方法

    ubuntu boot空间不足的解决方法 2013年12月11日 11:11:39 yypony 阅读数:17000 标签: ubuntu内存boot内核更多 个人分类: linux_usageubu ...

  5. 在 Java 8 中获取日期

    前言 前面一篇文章写了<SimpleDateFormat 如何安全的使用?>, 里面介绍了 SimpleDateFormat 如何处理日期/时间,以及如何保证线程安全,及其介绍了在 Jav ...

  6. 概率dp - UVA 11021 Tribles

    Tribles Problem's Link: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=33059 Mean: 有k个细 ...

  7. 日期-date.js

    Date.prototype.format = function(format) { var o = { "M+" : this.getMonth() + 1, // 月 &quo ...

  8. grid-tooltip扩展方法

    调用:$('#dg').datagrid('doCellTip', { 'max-width': '100px' }); /** * 扩展两个方法 */$.extend($.fn.datagrid.m ...

  9. iOS自动化测试需求实现(iOS按键精灵类似)

    需求分析: 作为以需求为驱动的IT公司,有再奇怪的需求都不奇怪,所以“24小时循测第三方应用”这样的需求也可以接受.业务需求重点为: 1.24小时循测 2.无人值守,自动完成 3.自动界面操作(点击. ...

  10. 【Python】python-memory-management

    http://deeplearning.net/software/theano/tutorial/python-memory-management.html#python-memory-managem ...