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 .

Example 1:

Input: "1 + 1"
Output: 2

Example 2:

Input: " 2-1 + 2 "
Output: 3

Example 3:

Input: "(1+(4+5+2)-3)+(6+8)"
Output: 23

Note:

  • You may assume that the given expression is always valid.
  • Do not use the eval built-in library function.
class Solution(object):
def calculate(self, s):
"""
:type s: str
:rtype: int
"""
sign = 1
res = 0
stack = []
index = 0
while index < len(s):
char = s[index]
if char.isdigit():
num = int(char)
while index + 1 < len(s) and s[index + 1].isdigit():
num = 10 * num + int(s[index + 1])
index += 1
res += sign * num
elif char == '+':
sign = 1
elif char == '-':
sign = -1
elif char == '(':
stack.append(res)
stack.append(sign)
res = 0
sign = 1
elif char == ')':
res = stack.pop() * res + stack.pop()
index += 1
return res
class Solution {
public int calculate(String s) {
char[] charArr = s.toCharArray();
LinkedList<Integer> stack = new LinkedList<>();
int sign = 1;
int num = 0;
for (int i = 0; i < charArr.length; i++) {
char cur = charArr[i];
if (Character.isDigit(cur)) {
int count = cur - '0';
while (i + 1 < charArr.length && Character.isDigit(charArr[i + 1])) {
// need to use charArr[i + 1]
count = 10 * count + charArr[i + 1] - '0';
i += 1;
}
num = num + count * sign;
} else if (cur == '+') {
sign = 1;
} else if (cur == '-') {
sign = -1;
} else if (cur == '(') {
stack.offerFirst(num);
stack.offerFirst(sign);
num = 0;
sign = 1;
} else if (cur == ')') {
num = num * stack.pollFirst() + stack.pollFirst();
}
}
return num;
}
}

[LC] 224. Basic Calculator的更多相关文章

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

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

  2. 224. Basic Calculator + 227. Basic Calculator II

    ▶ 两个四则表达式运算的题目,第 770 题 Basic Calculator IV 带符号计算不会做 Orz,第 772 题 Basic Calculator III 要收费 Orz. ▶ 自己的全 ...

  3. Java for LeetCode 224 Basic Calculator

    Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...

  4. (medium)LeetCode 224.Basic Calculator

    Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...

  5. 224. Basic Calculator

    题目: Implement a basic calculator to evaluate a simple expression string. The expression string may c ...

  6. [leetcode]224. Basic Calculator

    Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...

  7. 【LeetCode】224. Basic Calculator

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

  8. [LeetCode] 224. Basic Calculator 基本计算器

    Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...

  9. 【LeetCode】224. Basic Calculator 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 参考资料 日期 题目地址:https://lee ...

随机推荐

  1. 应用gis笔记

    接口,开发包??我要做一个移动端的,完了之后和5G挂一下钩, web/桌面/移动 C#就是.NET.... 和专业程序设计课程的区别 a kind of boring hope it helpful, ...

  2. RegressionTree(回归树)

    1.概述 回归树就是用树模型做回归问题,每一片叶子都输出一个预测值.预测值一般是该片叶子所含训练集元素输出的均值, 即 

  3. Java之同步方法处理继承Thread类的线程安全问题

    /** * 使用同步方法处理继承Thread类的方式中的线程安全问题 * */class Window4 extends Thread { private static int ticket = 10 ...

  4. GitHub 中 readme 如何添加图片

    一.Readme 是什么 readme文件一般是放在github 每个repo的根目录下,用来解释.说明本repo的主要内容和相关信息.而且在repo主页进去的时候会被自动加载.一般采用md标记的文本 ...

  5. axios新手实践实现登陆

    其实像这类的文章网上已经有很多很好的,写这篇文章,相当于是做个笔记,以防以后忘记 用到的:1. vuex 2.axios 3.vue-route 登陆流程为:1.提交登陆表单,拿到后台返回的数据 2. ...

  6. python模块——datetime

    datetime模块是python自带对时间的操作,其常用的四大类分别是date.time.datetime.timedelta.下面分别讲解下这四大类中常用的方法及其属性. date类 date类的 ...

  7. 两个tomcat使用同一个jvm可能会出错

    如果两个tomcat中的项目的某些类具有完全相同的包路径和类名的话,jvm可能会“弄混”这两个类,所以一般要求包名“必须”唯一. 当然,如果两个类中的代码和import的类完全一样,弄混了也就弄混了, ...

  8. CF 1096D Easy Problem [动态规划]

    题目链接:http://codeforces.com/problemset/problem/1096/D 题意: 有一长度为n的字符串,每一字符都有一个权值,要求现在从中取出若干个字符,使得字符串中没 ...

  9. linux xargs详解

    xargs  [-0prtx] [-E  eof-str] [-e[eof-str]] [--eof[=eof-str]] [--null] [-d delimiter] [--delimiter d ...

  10. Nginx模块-ngx_http_mirror_module-流量复制

    参考1:https://www.cnblogs.com/cjsblog/p/12163207.html Nginx流量复制 需求 将生产环境的流量拷贝到预上线环境或测试环境,这样做有很多好处,比如: ...