当前位置:网站首页>LeetCode-227-基本计算器||
LeetCode-227-基本计算器||
2022-07-23 09:25:00 【z754916067】
题目

思路
- 直接用栈,整两个栈,最后倒一下就可以。
代码
public int calculate(String s) {
//用栈存储
Stack<Integer> int_stack = new Stack<>();
Stack<Character> char_stack = new Stack<>();
for(int i=0;i<s.length();i++){
//如果是空格 跳过
Character c= s.charAt(i);
if(c==' ') continue;
//如果是数字 截取后面的数字
else if(c>='0' && c<='9'){
StringBuilder sb = new StringBuilder();
sb.append(c);
while((i+1)<s.length() && s.charAt(i+1)>='0' && s.charAt(i+1)<='9'){
sb.append(s.charAt(i+1));
i++;
}
//此刻栈顶是否为 * / 优先处理
if(char_stack.size()>0 && (char_stack.peek()=='*' || char_stack.peek()=='/')){
//该数字同栈顶数字做算式
int num1 = Integer.valueOf(sb.toString());
int num2 = int_stack.pop();
char op = char_stack.pop();
int ans=0;
if(op=='*') ans = num1*num2;
if(op=='/') ans = num2/num1;
int_stack.push(ans);
}else {
int_stack.push(Integer.valueOf(sb.toString()));
}
}
//如果是算式符
else if(c=='+' || c=='-' || c=='/' || c=='*'){
//压入栈中
char_stack.push(c);
}
}
//走到这里时 char_stack只存在+ - 进行运算即可
//此时int_stack和char应该倒过来
Stack<Integer> int_stack_1 = new Stack<>();
Stack<Character> char_stack_1 = new Stack<>();
while (!int_stack.empty()) int_stack_1.push(int_stack.pop());
while (!char_stack.empty()) char_stack_1.push(char_stack.pop());
while(!char_stack_1.empty()){
//取出char
Character c= char_stack_1.pop();
int num1 = int_stack_1.pop();
int num2 = int_stack_1.pop();
if(c=='+') int_stack_1.push(num1+num2);
if(c=='-') int_stack_1.push(num1-num2);
}
return int_stack_1.peek();
}
边栏推荐
- Qt|模仿文字浮动字母
- CSDN写文方法(二)
- Qt文档阅读笔记-Audio Example解析
- Quick introduction to PKI system
- [applet automation minium] i. framework introduction and environment construction
- 固定资产管理系统哪家好?固定资产管理平台有哪些?
- 【C語言】猜數字小遊戲+關機小程序
- CAN总线快速了解
- JS calendar style pie chart statistics plug-in
- Game (2) of 2022 Henan Mengxin League: solution to supplementary questions of Henan University of Technology
猜你喜欢

炫酷代码雨动态背景注册页面

JS calendar style pie chart statistics plug-in
![[download attached] several scripts commonly used in penetration testing that are worth collecting](/img/01/3b74c5ab4168059827230578753be5.png)
[download attached] several scripts commonly used in penetration testing that are worth collecting

基本51单片机点阵汉字显示程序设计

对象使用过程中背后调用了哪些方法

【FLink】FLink Hash collision on user-specified ID “opt“. Most likely cause is a non-unique ID
![webstrom ERROR in [eslint] ESLint is not a constructor](/img/e9/b084512d6aa8c4116d7068fdc8fc05.png)
webstrom ERROR in [eslint] ESLint is not a constructor

Flat style feedback form page

PKI体系快速介绍

Towhee weekly model
随机推荐
Game (2) of 2022 Henan Mengxin League: solution to supplementary questions of Henan University of Technology
Towhee weekly model
(重链剖分)魔法树
Chapter 3 complex query
运维高级作业03
Authing supports Zadig! Unified authentication and rapid docking of cloud native users
Optimisation du serveur Cloud Huawei avec connexion clé
在使用 VScode 进行代码格式化后,保存发现代码又变乱了,怎么办?vs去掉格式化
生成订单号
基金开户网上办理是否安全?谁给解答一下
CSDN writing method (II)
Using JS to parse and execute XSS automatically
Quanzhi f1c100s/f1c200s learning notes (13) -- lvgl transplantation
AI acceleration gesture recognition experience based on efr32mg24
Aruba学习笔记05-配置架构- WLAN配置架构
Looking for peak [Abstract dichotomy exercise]
因为资源限制,导致namenode启动失败,报错unable to create new native thread
【数组&&字符串&&宏练习题】
【小程序自动化Minium】一、框架介绍和环境搭建
[applet automation minium] i. framework introduction and environment construction