当前位置:网站首页>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;
}
}
边栏推荐
- Pulsar的Proxy支持和SNI路由
- 鼠标悬停效果六
- Codeforces Round #416 (Div. 2) C. Vladik and Memorable Trip
- Machine learning 9-universal approximator radial basis function neural network, examining PDA and SVM from a new perspective
- The latest wechat iPad protocol code obtains official account authorization, etc
- 记一次服务部署失败问题排查
- Image preloading in JS
- import tensorflow. contrib. Slim as slim error
- Restcloud ETL WebService data synchronization to local
- 7_OpenResty安装
猜你喜欢

Here comes the share creators budding talent training program!

Introduction to kubernetes resource objects and common commands (II)

Focusing on green and low carbon, data center cooling has entered a new era of "intelligent cooling"

How to use Jieba participle in unity

视觉特效,图片转成漫画功能

记一次服务部署失败问题排查

Xception learning notes

The latest wechat iPad protocol code obtains official account authorization, etc

Gartner research: in China, the adoption of hybrid cloud has become the mainstream trend

Dell server restart Idrac method
随机推荐
Mouse over effect V
Restcloud ETL practice to realize incremental data synchronization without identification bit
Xception learning notes
ssh配置免密登录时报错:/usr/bin/ssh-copy-id: ERROR: No identities found 解决方法
Restcloud ETl数据通过时间戳实现增量数据同步
Here comes the share creators budding talent training program!
Mouse over effect 9
联想X86服务器重启管理控制器(XClarity Controller)或TSM的方法
import tensorflow. contrib. Slim as slim error
Voici le programme de formation des talents de SHARE Creators!
5款主流智能音箱入门款测评:苹果小米华为天猫小度,谁的表现更胜一筹?
Restcloud ETL data realizes incremental data synchronization through timestamp
如何在智汀中实现智能锁与灯、智能窗帘电机场景联动?
How to buy Hong Kong shares in China? What platform is safer?
Gartner研究:在中国,混合云的采用已成为主流趋势
Complete training and verification of a neural network based on pytorch
Small program cloud development -- wechat official account article collection
RestCloud ETL实践之无标识位实现增量数据同步
最新微信ipad协议 CODE获取 公众号授权等
PCB defect detection based on OpenCV and image subtraction