当前位置:网站首页>Basic calculator II for leetcode topic analysis
Basic calculator II for leetcode topic analysis
2022-06-23 05:50:00 【ruochen】
Implement a basic calculator to evaluate a simple expression string.
The expression string contains only non-negative integers, +, -, *, / operators and empty spaces . The integer division should truncate toward zero.
You may assume that the given expression is always valid.
Some examples:
"3+2*2" = 7
" 3/2 " = 1
" 3+5 / 2 " = 5
Note: Do not use the eval built-in library function.
Go through it twice , The first time I traverse , The multiplication and division symbols are used to calculate ; Second traversal , Calculating addition and subtraction symbols .
public int calculate(String s) {
if (s == null || s.length() == 0) {
return 0;
}
Stack<Integer> stack = new Stack<Integer>();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (Character.isDigit(c)) {
int val = c - '0';
// Take out the number
while (i + 1 < s.length() && Character.isDigit(s.charAt(i + 1))) {
val = val * 10 + s.charAt(++i) - '0';
}
// At the top of the stack * / Operator , Calculation
if (!stack.isEmpty()
&& (stack.peek() == 2 || stack.peek() == 3)) {
int sign = stack.pop();
int op = stack.pop();
int rt = 0;
if (sign == 2) {
rt = op * val;
} else {
rt = op / val;
}
stack.push(rt);
} else {
stack.push(val);
}
} else if (c == ' ') {
continue;
} else {
switch (c) {
case '+':
stack.push(0);
break;
case '-':
stack.push(1);
break;
case '*':
stack.push(2);
break;
case '/':
stack.push(3);
break;
}
}
}
if (stack.isEmpty()) {
return 0;
}
// Because it needs to be calculated from left to right , So we need to reverse
Collections.reverse(stack);
// Calculation +-
int rt = stack.pop();
while (!stack.isEmpty()) {
int sign = stack.pop();
int op = stack.pop();
if (sign == 0) {
rt += op;
} else {
rt -= op;
}
}
return rt;
}边栏推荐
- MySQL面试真题(二十二)——表连接后的条件筛选及分组筛选
- Wechat applet; AI intelligent dubbing assistant
- MySQL面试真题(二十七)——RFM分析法对用户进行分类
- Real MySQL interview question (23) -- pinduoduo ball game analysis
- 华为软硬件生态圈成型,从根子上改变美国对软硬件体系的领导地位
- Yingjixin ip5566 with type-C port 3A charging and discharging fast charging mobile power supply 5W wireless charging in one SOC
- Wechat applet: future wife query generator
- What does the English letter PC mean? What does the Internet PC mean
- Advanced Mathematics (Seventh Edition) Tongji University exercises 1-8 personal solutions
- PAT 乙等 1022 D进制的A+B
猜你喜欢

新课上线 | 每次 5 分钟,轻松玩转阿里云容器服务!

True MySQL interview question (24) -- row column exchange

June 22, 2022: golang multiple choice question, what does the following golang code output? A:3; B:1; C:4; D: Compilation failed. package main import ( “fmt“ ) func mai

数字藏品火热背后需要强大的技术团队支持 北方技术团队

Wechat applet: an artifact for calculating the full amount of orders

What does the English letter PC mean? What does the Internet PC mean

数字藏品到底有什么魔力?目前有哪些靠谱的团队在开发

Activity启动模式和生命周期实测结果

Build a gocd environment

Opportunities and challenges of digital collections from the perspective of technology development team
随机推荐
Mobile power fast charging qc3.0 scheme chip ip5318 fast charging scheme
数字化工厂建设可划分为三个方面
PAT 乙等 1011 C语言
Genetic engineering of AI art? Use # artbreeder to change any shape of the image
PAT 乙等 1012 C语言
sprintf 格式代码使用不规范在不同平台下的表现
Heimdall Database Proxy横向扩展提高20倍
node中操作mongoDB
The performance of nonstandard sprintf code in different platforms
数字藏品如何赋能经济实体?
Wechat applet: Puzzle toolbox
Jsonfield annotation in fastjson
Kotlin android简单Activity跳转、handler和thread简单配合使用
Real MySQL interview questions (XXVII) -- Classification of users by RFM analysis method
visdom的使用
制造业数字化转型存在问题及原因分析
MySQL Foundation
PAT 乙等 1017 C语言
Ansible 使用普通用户管理被控端
Opencv display image