当前位置:网站首页>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;
}
}
边栏推荐
- How do I open an account on my mobile phone? Also, is it safe to open an account online?
- 522. Longest special sequence II
- 联想X86服务器重启管理控制器(XClarity Controller)或TSM的方法
- 如何在智汀中实现智能锁与灯、智能窗帘电机场景联动?
- Introduction to kubernetes resource objects and common commands (II)
- RestCloud ETL WebService数据同步到本地
- The operation efficiency of the park is improved, and the application platform management of applet container technology is accelerated
- map数组函数
- 在国内如何买港股的股?用什么平台安全一些?
- kubernetes资源对象介绍及常用命令(二)
猜你喜欢

Sampling Area Lights

园区运营效率提升,小程序容器技术加速应用平台化管理

centos 安装多个版本的php并切换

Gartner研究:在中国,混合云的采用已成为主流趋势

Machine learning 9-universal approximator radial basis function neural network, examining PDA and SVM from a new perspective

Pychar open remote directory remote host

Restcloud ETL data realizes incremental data synchronization through timestamp

MCU firmware packaging Script Software

php批量excel转word

Pychart software deployment gray unable to point
随机推荐
Evaluation of the entry-level models of 5 mainstream smart speakers: apple, Xiaomi, Huawei, tmall, Xiaodu, who is better?
Pycharm 打开远程目录 Remote Host
Résumé des styles de développement d'applets Wechat
Restcloud ETL data realizes incremental data synchronization through timestamp
Mouse over effect I
Use ipmitool to configure BMC network and user information of X86 server
import tensorflow. contrib. Slim as slim error
如果在小券商办理网上开户安全吗?我的资金会不会不安全?
大橙子疯博客搬家通知
Focusing on green and low carbon, data center cooling has entered a new era of "intelligent cooling"
Sampling Area Lights
Densenet network paper learning notes
ANR问题的分析与解决思路
基于OPENCV和图像减法的PCB缺陷检测
Image preloading in JS
The mobile edge browser cannot open the third-party application
7_ Openresty installation
SAP ALV summary is inconsistent with exported excel summary data
Why are strings immutable in many programming languages? [repeated] - why are strings immutable in many programming languages? [duplicate]
Nacos configuration center tutorial