当前位置:网站首页>Leetcode-227-basic calculator||
Leetcode-227-basic calculator||
2022-07-23 14:53:00 【z754916067】
subject

Ideas
- Just use the stack , The whole two stacks , Just pour it down at last .
Code
public int calculate(String s) {
// Stack storage
Stack<Integer> int_stack = new Stack<>();
Stack<Character> char_stack = new Stack<>();
for(int i=0;i<s.length();i++){
// If it's a space skip
Character c= s.charAt(i);
if(c==' ') continue;
// If it's a number Intercept the following numbers
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++;
}
// Is the top of the stack at this moment * / priority
if(char_stack.size()>0 && (char_stack.peek()=='*' || char_stack.peek()=='/')){
// This number is the same as the number at the top of the stack
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()));
}
}
// If it is an operator
else if(c=='+' || c=='-' || c=='/' || c=='*'){
// Pressure into the stack
char_stack.push(c);
}
}
// When I got here char_stack There is only + - Just do the operation
// here int_stack and char It should be reversed
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()){
// Take out 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();
}
边栏推荐
- After using vscode to format the code, save and find that the code is messy again. What should I do? Vs remove formatting
- [C language] number guessing game + shutdown applet
- [test platform development] XVII. The interface editing page realizes the drop-down cascade selection, and binds the module to which the interface belongs
- 【软件测试】如何梳理你测试的业务
- 云呐|怎样管理固定资产?如何进行固定资产管理?
- [test platform development] 21. complete sending interface request and display response header information
- [applet automation minium] i. framework introduction and environment construction
- 真人踩过的坑,告诉你避免自动化测试常犯的10个错误
- Mathematical function of MySQL function summary
- 转自玉溪信息公开:mRNA新冠疫苗、九洲马破伤风免疫球蛋白等产品有望年内上市。
猜你喜欢

uni-app知识点和项目上遇到的问题和解决办法的记录

工作小记:一次抓包

基本51单片机点阵汉字显示程序设计
![[test platform development] XVII. The interface editing page realizes the drop-down cascade selection, and binds the module to which the interface belongs](/img/19/de8f4de5adbfcfb62291b8664187fd.png)
[test platform development] XVII. The interface editing page realizes the drop-down cascade selection, and binds the module to which the interface belongs
![Looking for peak [Abstract dichotomy exercise]](/img/99/122e79784f0f07120680d2cbcf89da.png)
Looking for peak [Abstract dichotomy exercise]

104 maximum depth of binary tree and 543 diameter of binary tree and 124 maximum path sum of binary tree

【测试平台开发】23. 接口断言功能-保存接口断言和编辑回显

C thread lock and single multithreading are simple to use

Sword finger offer19 regular expression
![[applet automation minium] i. framework introduction and environment construction](/img/1f/95b78e6574c3af3ff7abcf5db838f5.png)
[applet automation minium] i. framework introduction and environment construction
随机推荐
CAN总线快速了解
Design and implementation of websocket universal packaging
Ffmpeg 1 - Overview / installation
一道代码题看 CommonJS 模块化规范
Uni app knowledge points and records of problems and solutions encountered in the project
@FeignClient使用詳細教程(圖解)
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
利用js自动解析执行xss
C language implements memcpy and memmove
Zhongwang CAD professional 2022 software installation package download and installation tutorial
Game (2) of 2022 Henan Mengxin League: solution to supplementary questions of Henan University of Technology
4. Find the median of two positive arrays
俄方希望有效落实农产品外运“一揽子”协议
Canvas from getting started to persuading friends to give up (graphic version)
MySQL unique index has no duplicate value, and the error is repeated
Regular expression common syntax parsing
21 - 二叉树的垂直遍历
C language implements StrCmp, strstr, strcat, strcpy
对象使用过程中背后调用了哪些方法
Quick introduction to PKI system