当前位置:网站首页>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();
}
边栏推荐
- 因为资源限制,导致namenode启动失败,报错unable to create new native thread
- 什么是Per-Title编码?
- (重链剖分)魔法树
- 【刷题记录】19. 删除链表的倒数第 N 个结点
- 基金开户网上办理是否安全?谁给解答一下
- Yunna | how to manage the fixed assets of the company? How to manage the company's fixed assets better?
- 炫酷代码雨动态背景注册页面
- 基本51单片机点阵汉字显示程序设计
- FFmpeg 2 - ffplay、ffprobe、ffmpeg 命令使用
- Uni app knowledge points and records of problems and solutions encountered in the project
猜你喜欢

10年软件测试工程师经验,很茫然....

優化華為雲服務器采用Key登陸

什麼是Per-Title編碼?

Question 142 of Li Kou: circular linked list 2

Which is a good fixed asset management system? What are the fixed asset management platforms?

About flex layout justify content: the last solution to the misalignment of space around and why it is solved like this is a discussion

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

【测试平台开发】二十、完成编辑页发送接口请求功能

Dynamic programming -- knapsack problem
![[C language] number guessing game + shutdown applet](/img/2f/643ec964dba7643f91b1bd679a9284.png)
[C language] number guessing game + shutdown applet
随机推荐
【附下载】值得收藏的几款渗透测试常用的脚本
对象使用过程中背后调用了哪些方法
[download attached] several scripts commonly used in penetration testing that are worth collecting
[WinForm] desktop program implementation scheme for screenshot recognition and calculation
Dynamic programming -- knapsack problem
运维高级作业02
【我可以做你的第一个项目吗?】GZIP的细节简介和模拟实现
R language practical application case: drawing part (III) - drawing of multiple combination patterns
【测试平台开发】21. 完成发送接口请求显示响应头信息
LZ77文件压缩
第4章 集合运算
【测试平台开发】23. 接口断言功能-保存接口断言和编辑回显
uni-app知识点和项目上遇到的问题和解决办法的记录
104 maximum depth of binary tree and 543 diameter of binary tree and 124 maximum path sum of binary tree
云呐-如何加强固定资产管理?怎么加强固定资产管理?
【测试平台开发】二十、完成编辑页发送接口请求功能
CSDN写文方法(二)
身份证号正则验证
Regular verification of ID number
【FLink】FLink Hash collision on user-specified ID “opt“. Most likely cause is a non-unique ID