[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
evalbuilt-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 ...
随机推荐
- vue安装openlayers,jquery,bootstrap,阿里iconfont,
安装 安装openlayers安装指定包安装openlayersVUE中的地图import ol from "openlayers";import "openlayers ...
- Wallet file not specified (must request wallet RPC through /wallet/<filename> uri-path). BitcoinJSONRPCClient异常、及其他异常
1.异常信息 Wallet file not specified (must request wallet RPC through /wallet/<filename> uri-path) ...
- 黑马_13 Spring Boot:05.spring boot 整合其他技术
13 Spring Boot: 01.spring boot 介绍&&02.spring boot 入门 04.spring boot 配置文件 05.spring boot 整合其他 ...
- UML-交互图包含哪些图?
猫比狗精明,但你无法让8只猫在雪地里拉雪橇---杰夫.瓦尔德斯 本章是重点. 1.总览 2.顺序图 1).类A具有doOne方法和类B的属性 2).doOne方法中调用类B的doTwo()和doThr ...
- 常用的模型集成方法介绍:bagging、boosting 、stacking
本文介绍了集成学习的各种概念,并给出了一些必要的关键信息,以便读者能很好地理解和使用相关方法,并且能够在有需要的时候设计出合适的解决方案. 本文将讨论一些众所周知的概念,如自助法.自助聚合(baggi ...
- Teensy-HID攻击
title date tags layut 渗透利器-Teensy(低配版BadUSB) 2018-09-25 kali post 准备工作 一块 Teensy2.0++ 的板子(淘宝一搜就有) Ar ...
- 1.windows-oracle实战第一课
一.oracle是目前最流行的数据库之一,功能强大.性能卓越.学习要有信心.oracle也做软件,不仅仅是数据库.比如ERP(企业资源计划,用友.金蝶) 二.目前的数据库 相对而言: ...
- Python常见经典
python中if __name__ == '__main__': 的解析 当你打开一个.py文件时,经常会在代码的最下面看到if __name__ == '__main__':,现在就来介 绍一下它 ...
- \b 是单词边界锚点 word-boundary anchor,一个“\b”匹配一个单词的一端,两个“\b”匹配一个单词的头尾两端
123 $_ = "beforematcha? fter"; 124 if(/\b\w+a\b/){ 125 print "matched: < ...
- Laravel 操作指令
php artisan migrate —path=database/migrations/v1 更新表数据 php artisan make:migration create_channels_ta ...