当前位置:网站首页>LeetCode_栈_困难_227.基本计算器(不含乘除)
LeetCode_栈_困难_227.基本计算器(不含乘除)
2022-07-01 02:45:00 【一瓢江湖我沉浮】
1.题目
给你一个字符串表达式 s ,请你实现一个基本计算器来计算并返回它的值。
注意:不允许使用任何将字符串作为数学表达式计算的内置函数,比如 eval() 。
示例 1:
输入:s = “1 + 1”
输出:2
示例 2:
输入:s = " 2-1 + 2 "
输出:3
示例 3:
输入:s = “(1+(4+5+2)-3)+(6+8)”
输出:23
提示:
1 <= s.length <= 3 * 105
s 由数字、‘+’、‘-’、‘(’、‘)’、和 ’ ’ 组成
s 表示一个有效的表达式
‘+’ 不能用作一元运算(例如,“+1” 和 “+(2 + 3)” 无效)
‘-’ 可以用作一元运算(即 “-1” 和 “-(2 + 3)” 是有效的)
输入中不存在两个连续的操作符
每个数字和运行的计算将适合于一个有符号的 32 位整数
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/basic-calculator
2.思路
(1)栈
思路参考本题官方题解。
3.代码实现(Java)
//思路1————栈
class Solution {
public static int calculate(String s) {
int res = 0;
int length = s.length();
//sign 记录每个数之前的符号,对于表达式中的第一个数,默认其符号为 '+',即用 1 表示
int sign = 1;
Stack<Integer> opsStack = new Stack<>();
opsStack.push(1);
for (int i = 0; i < length; ) {
switch (s.charAt(i)) {
case ' ':
//遇到空格时直接跳过
i++;
break;
case '+':
sign = opsStack.peek();
i++;
break;
case '-':
sign = -opsStack.peek();
i++;
break;
case '(':
opsStack.push(sign);
i++;
break;
case ')':
opsStack.pop();
i++;
break;
default:
//表示遍历表达式时遇到的每一个数
int num = 0;
while (i < length && Character.isDigit(s.charAt(i))) {
num = num * 10 + (s.charAt(i) - '0');
i++;
}
res += sign * num;
break;
}
}
return res;
}
}
边栏推荐
- Lenovo x86 server restart management controller (xclarity controller) or TSM method
- Pychar open remote directory remote host
- 如果我在北京,到哪里开户比较好?另外,手机开户安全么?
- 手机上怎么开户?还有,在线开户安全么?
- 522. Longest special sequence II
- nacos配置中心使用教程
- 最新微信ipad协议 CODE获取 公众号授权等
- [graduation season · advanced technology Er] - summary from graduation to work
- RestCloud ETL实践之无标识位实现增量数据同步
- Contrastive learning of Class-agnostic Activation Map for Weakly Supervised Object Localization and
猜你喜欢
Restcloud ETl数据通过时间戳实现增量数据同步
[2022] Jiangxi postgraduate mathematical modeling scheme and code
js中的原型和原型链
The mobile edge browser cannot open the third-party application
Optimal transport Series 1
Machine learning 9-universal approximator radial basis function neural network, examining PDA and SVM from a new perspective
记一次服务部署失败问题排查
单片机 MCU 固件打包脚本软件
Contrastive learning of Class-agnostic Activation Map for Weakly Supervised Object Localization and
Gartner research: in China, the adoption of hybrid cloud has become the mainstream trend
随机推荐
Pulsar theme compression
robots. Txt restrict search engine inclusion
AI edge computing platform - beaglebone AI 64 introduction
Evaluation of the entry-level models of 5 mainstream smart speakers: apple, Xiaomi, Huawei, tmall, Xiaodu, who is better?
Youmeng (a good helper for real-time monitoring of software exceptions: crash) access tutorial (the easiest tutorial for Xiaobai with some foundation)
鼠标悬停效果六
servlet【初识】
Optimal transport Series 1
最新微信ipad协议 CODE获取 公众号授权等
C # generates PPK files in putty format (supports passphrase)
在unity中使用jieba分词的方法
Clickhouse eliminates the gap caused by group by
Sampling Area Lights
Borrowing constructor inheritance and composite inheritance
Nacos configuration center tutorial
robots.txt限制搜索引擎收录
Desai wisdom number - other charts (parallel coordinate chart): employment of fresh majors in 2021
centos 安装多个版本的php并切换
Mouse over effect IV
Dell服务器重启iDRAC方法