当前位置:网站首页>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();
}
边栏推荐
- Palindrome related topics
- 第2章 基础查询与排序
- 炫酷代码雨动态背景注册页面
- Which is a good fixed asset management system? What are the fixed asset management platforms?
- Is it risky and safe to open a mobile stock account?
- uni-app知识点和项目上遇到的问题和解决办法的记录
- Chapter 3 complex query
- koa框架的使用
- 【刷题记录】19. 删除链表的倒数第 N 个结点
- 【FLink】FLink Hash collision on user-specified ID “opt“. Most likely cause is a non-unique ID
猜你喜欢
![[applet automation minium] III. element positioning - use of wxss selector](/img/ec/51eadd08bea18f8292aa3521f11e8a.png)
[applet automation minium] III. element positioning - use of wxss selector

Spotlight light box JS plug-in full screen enlarged picture

扁平样式反馈表单页面

什么是Per-Title编码?

优化华为云服务器采用Key登陆

Okrk3399 Development Board reserves i2c4 to mount EEPROM
![webstrom ERROR in [eslint] ESLint is not a constructor](/img/e9/b084512d6aa8c4116d7068fdc8fc05.png)
webstrom ERROR in [eslint] ESLint is not a constructor

【数组&&字符串&&宏练习题】

Question 142 of Li Kou: circular linked list 2

JS calendar style pie chart statistics plug-in
随机推荐
Vk36n5d anti power interference / mobile phone interference 5-key 5-channel touch detection chip anti freeze function ponding in the touch area can still be operated
微信官方出品!小程序自动化框架 minium 分享预告
Using JS to parse and execute XSS automatically
转自玉溪信息公开:mRNA新冠疫苗、九洲马破伤风免疫球蛋白等产品有望年内上市。
C# 线程锁和单多线程简单使用
Solve a series of problems in using Bert encoder
炫酷代码雨动态背景注册页面
AI acceleration gesture recognition experience based on efr32mg24
优化华为云服务器采用Key登陆
Qt文档阅读笔记-Audio Example解析
[can I do your first project?] Detailed introduction and Simulation Implementation of gzip
It is suggested that Siyuan notes can be compatible with third-party sync disks
10 years of software testing engineer experience, very confused
C语言入门实战(11):输入一组正整数,求逆序数的和
CSDN writing method (II)
【软件测试】如何梳理你测试的业务
Program design of dot matrix Chinese character display of basic 51 single chip microcomputer
Chapter 3 complex query
基本51单片机点阵汉字显示程序设计
[applet automation minium] i. framework introduction and environment construction