当前位置:网站首页>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();
}
边栏推荐
- 直播课堂系统02-搭建项目环境
- Oracle 报表常用sql
- R language practical application case: drawing part (III) - drawing of multiple combination patterns
- 固定资产管理系统哪家好?固定资产管理平台有哪些?
- [WinForm] desktop program implementation scheme for screenshot recognition and calculation
- Zhongwang CAD professional 2022 software installation package download and installation tutorial
- 【小程序自动化Minium】一、框架介绍和环境搭建
- 【C语言】猜数字小游戏+关机小程序
- 【测试平台开发】23. 接口断言功能-保存接口断言和编辑回显
- 可以进行2D或3D三角剖分的一些库
猜你喜欢

C# 线程锁和单多线程简单使用

运维高级作业03
![Looking for peak [Abstract dichotomy exercise]](/img/99/122e79784f0f07120680d2cbcf89da.png)
Looking for peak [Abstract dichotomy exercise]
![[can I do your first project?] Detailed introduction and Simulation Implementation of gzip](/img/92/dfef5887e2313f8025baf11c8f4379.png)
[can I do your first project?] Detailed introduction and Simulation Implementation of gzip

Authing supports Zadig! Unified authentication and rapid docking of cloud native users
![[interview frequency] cookies, sessions, tokens? Don't worry about being asked after reading it](/img/c0/46f2a99ef2ed0b5d4d5c77a143b160.png)
[interview frequency] cookies, sessions, tokens? Don't worry about being asked after reading it
![[applet automation minium] i. framework introduction and environment construction](/img/1f/95b78e6574c3af3ff7abcf5db838f5.png)
[applet automation minium] i. framework introduction and environment construction

直播课堂系统03补充-model类及实体

Transferred from Yuxi information disclosure: products such as mRNA covid-19 vaccine and Jiuzhou horse tetanus immunoglobulin are expected to be on the market within this year.

Which is a good fixed asset management system? What are the fixed asset management platforms?
随机推荐
Program design of dot matrix Chinese character display of basic 51 single chip microcomputer
Yunna | how to manage fixed assets? How to manage fixed assets?
CSDN writing method (II)
C# 线程锁和单多线程简单使用
Yunna | how to manage the fixed assets of the company? How to manage the company's fixed assets better?
[interview frequency] cookies, sessions, tokens? Don't worry about being asked after reading it
[paper notes] mobile robot navigation method based on hierarchical depth reinforcement learning
【小程序自动化Minium】三、元素定位- WXSS 选择器的使用
C language implements StrCmp, strstr, strcat, strcpy
可以进行2D或3D三角剖分的一些库
身份证号正则验证
Opencv calculation outsourcing rectangle
云呐|怎样管理固定资产?如何进行固定资产管理?
mysql函数汇总之数学函数
4. Find the median of two positive arrays
104 maximum depth of binary tree and 543 diameter of binary tree and 124 maximum path sum of binary tree
Wacom firmware update error 123, digital board driver cannot be updated
Zhongwang CAD professional 2022 software installation package download and installation tutorial
String function of MySQL function summary
[array & String & Macro exercise]