【LeetCode】224. Basic Calculator
Basic Calculator
Implement a basic calculator to evaluate a simple expression string.
The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces .
You may assume that the given expression is always valid.
Some examples:
"1 + 1" = 2
" 2-1 + 2 " = 3
"(1+(4+5+2)-3)+(6+8)" = 23
Note: Do not use the eval built-in library function.
两个要点:
1、无括号时,顺序执行
2、有括号时,先执行括号中的
两个栈:
一个存放操作数,每次进栈要注意,如果操作符栈顶元素为'+'/'-',则需要立即计算。
一个存放操作符(包括括号),每次出现')'时,不断进行出栈计算再进栈,直到弹出'(',说明当前括号内计算完毕。
class Solution {
public:
int calculate(string s) {
stack<int> num;
stack<int> op;
int i = ;
while(i < s.size())
{
while(i < s.size() && s[i] == ' ')
i ++;
if(i == s.size())
break;
if(s[i] == '+' || s[i] == '-' || s[i] == '(')
{
op.push(s[i]);
i ++;
}
else if(s[i] == ')')
{
while(op.top() != '(')
{// calculation within parentheses
int n2 = num.top();
num.pop();
int n1 = num.top();
num.pop();
if(op.top() == '+')
num.push(n1 + n2);
else
num.push(n1 - n2);
op.pop();
}
op.pop();
while(!op.empty() && op.top() != '(')
{
int n2 = num.top();
num.pop();
int n1 = num.top();
num.pop();
if(op.top() == '+')
num.push(n1 + n2);
else
num.push(n1 - n2);
op.pop();
}
i ++;
}
else
{
int n = ;
while(i < s.size() && s[i] >= '' && s[i] <= '')
{
n = n* + (s[i]-'');
i ++;
}
num.push(n);
while(!op.empty() && op.top() != '(')
{
int n2 = num.top();
num.pop();
int n1 = num.top();
num.pop();
if(op.top() == '+')
num.push(n1 + n2);
else
num.push(n1 - n2);
op.pop();
}
}
}
return num.top();
}
};

【LeetCode】224. Basic Calculator的更多相关文章
- 【LeetCode】224. Basic Calculator 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 参考资料 日期 题目地址:https://lee ...
- 【LeetCode】227. Basic Calculator II 解题报告(Python)
[LeetCode]227. Basic Calculator II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- 【刷题-LeetCode】224. Basic Calculator
Basic Calculator Implement a basic calculator to evaluate a simple expression string. The expression ...
- 【LeetCode】227. Basic Calculator
Problem: Implement a basic calculator to evaluate a simple expression string. The expression string ...
- 【LeetCode】227. Basic Calculator II
Basic Calculator II Implement a basic calculator to evaluate a simple expression string. The express ...
- 【LeetCode】991. Broken Calculator 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【leetcode】991. Broken Calculator
题目如下: On a broken calculator that has a number showing on its display, we can perform two operations ...
- leetcode 224. Basic Calculator 、227. Basic Calculator II
这种题都要设置一个符号位的变量 224. Basic Calculator 设置数值和符号两个变量,遇到左括号将数值和符号加进栈中 class Solution { public: int calcu ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
随机推荐
- 集成学习总结 & Stacking方法详解
http://blog.csdn.net/willduan1/article/details/73618677 集成学习主要分为 bagging, boosting 和 stacking方法.本文主要 ...
- 微信小程序 this.data与this.setData
一.摘要 小程序中我们会经常使用到this.data与this.setData.其中this.data是用来获取页面data对象的,而this.setData是用来更新界面的.那么他们之间的区别与联系 ...
- 【转】字符编码笔记:ASCII,Unicode 和 UTF-8
原文:http://www.ruanyifeng.com/blog/2007/10/ascii_unicode_and_utf-8.html https://www.key-shortcut.com/ ...
- 解决ThinkPHP的Create方法失效而没有提示错误信息的问题
ThinkPHP中的数据创建Create方法是一个非常有用的功能,它自动根据表单数据创建数据对象(在表字段很多的情况下尤其明显) 但有时候该方法可能并未按照你期望的来工作,比如方法不工作而且还没有提示 ...
- Simple XOR Encryption/Decryption in C++ (And Several Other Languages)
For details on how to implement XOR encryption using Go, see this post. If you are looking for XOR e ...
- [Jade] Piped text
Another way to add plain text to templates is to prefix a line with a pipe character (|). This metho ...
- maven 下载源码downloadsources
mvn eclipse:eclipse -Ddownloadsources=true -Ddownloadjavadocs=true
- iOS开发技巧 - 使用UIPickerView来选择值
(Swift) import UIKit class ViewController: UIViewController, UIPickerViewDataSource { var picker: UI ...
- 牛客网-《剑指offer》-二维数组中的查找
题目:http://www.nowcoder.com/practice/abc3fe2ce8e146608e868a70efebf62e C++ class Solution { public: bo ...
- poj2689 Prime Distance 有难度 埃拉托斯尼斯筛法的运用
我承认这道很难(对我来说),搞脑子啊,搞了好久,数论刚开始没多久,还不是很强大,思路有点死,主要是我 天赋太差,太菜了,希望多做做有所改善 开始解析: 首先要将在 [ l,u]内的所有素数找出来,还好 ...