[LC] 224. 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 .
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的更多相关文章
- leetcode 224. Basic Calculator 、227. Basic Calculator II
这种题都要设置一个符号位的变量 224. Basic Calculator 设置数值和符号两个变量,遇到左括号将数值和符号加进栈中 class Solution { public: int calcu ...
- 224. Basic Calculator + 227. Basic Calculator II
▶ 两个四则表达式运算的题目,第 770 题 Basic Calculator IV 带符号计算不会做 Orz,第 772 题 Basic Calculator III 要收费 Orz. ▶ 自己的全 ...
- Java for LeetCode 224 Basic Calculator
Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...
- (medium)LeetCode 224.Basic Calculator
Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...
- 224. Basic Calculator
题目: Implement a basic calculator to evaluate a simple expression string. The expression string may c ...
- [leetcode]224. Basic Calculator
Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...
- 【LeetCode】224. Basic Calculator
Basic Calculator Implement a basic calculator to evaluate a simple expression string. The expression ...
- [LeetCode] 224. Basic Calculator 基本计算器
Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...
- 【LeetCode】224. Basic Calculator 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 参考资料 日期 题目地址:https://lee ...
随机推荐
- Java--二维码生成&图片和流转化
package test; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java. ...
- Django2.0——实现简易登陆、注册
思路: 实现简易的登陆.注册,我们至少需要三个HTML页面,一个主页面.一个登陆界面.一个注册界面.为了存储和校验用户的账号和密码,我们需要写一个模型类(用于映射到数据库).两个form类(一个登陆. ...
- JS中,输出1-10之间的随机整数
<script> document.write(parseInt(10*Math.random())); //输出0-10之间的随机整数 document.write(Math.floor ...
- RedHat6.5升级内核
redhat6.5 升级内核 1.导入key rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org 2.安装elrepo的yum源 rp ...
- .jar文件不能解析、识别
- C++常用库函数 C函数库 cstdio
常用的C/C++函数库, cstdio(stdio.h) 标准输入输出库.C Standard Input and Output Library 1. 实例 #include <cstdio&g ...
- blocking(非阻塞)回调函数
回调函数不会造成阻塞 function loop() { setTimeout(loop, 0) } loop 死循环 while(true)
- springboot配置多个yml文件
新接触了springboot项目,yml一大堆,启动不知道用的哪个,各种百度后: <profiles> <profile> <id>dev</id> & ...
- epoll——IO多路复用选择器
上上篇博客讲的套接字,由于其阻塞性而导致一个服务端同一时间只能与一个客户端连接.基于这个缺点,在上篇博客我们将其设置为非阻塞实现了一个服务端同一时间可以与多个客户端相连,即实现了并发,但其同样留下了一 ...
- 887C. Slava and tanks#轰炸弹坦克游戏(分析)
题目出处:http://codeforces.com/problemset/problem/877/C 题目大意:按照游戏规则,求最小炸弹使用次数 #include<iostream> u ...