当前位置:网站首页>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: an artifact for calculating the full amount of orders
- TCP/IP 详解(第 2 版) 笔记 / 3 链路层 / 3.3 全双工, 节能, 自动协商机制, 802.1X 流控制 / 3.3.3 链路层流量控制
- Redis cache penetration solution - bloom filter
- PAT 乙等 1026 程序运行时间
- 【owt】owt-client-native-p2p-e2e-test vs2017构建 6:修改脚本自动生成vs工程
- What does the English letter PC mean? What does the Internet PC mean
- AI艺术的基因工程?使用 #Artbreeder 改变图像的任意形态
- Opportunities and challenges of digital collections from the perspective of technology development team
- Jvm: when a method is overloaded, the specific method to call is determined by the static type of the incoming parameter rather than the actual type of the parameter
猜你喜欢

Real MySQL interview question (30) -- shell real estate order analysis

Software design and Development Notes 2: serial port debugging tool based on QT design

数字化工厂建设可划分为三个方面

Wechat applet: wechat can also send flash photos to create wechat applet source code download and customize flash time

MDM data cleaning function development description

What is the magic of digital collections? Which reliable teams are currently developing

Yingjixin ip6806 wireless charging scheme 5W Qi certified peripheral simplified 14 devices

C primer plus学习笔记 —— 2、常量与格式化IO(输入/输出)

visdom画多条动态损失曲线

树莓派assert初步使用练习
随机推荐
Lottery DDD code
[Stanford Jiwang cs144 project] lab2: tcpreceiver
A bit of knowledge - folding forging and Damascus steel
MDM data cleaning function development description
Differences between fs4059a and fs5080e charging chips
Lihongyi, machine learning 5 Tips for neural network design
MySQL面试真题(二十五)——常见的分组比较场景
MySQL面试真题(二十八)——案例-通讯运营商指标分析
Jvm: when a method is overloaded, the specific method to call is determined by the static type of the incoming parameter rather than the actual type of the parameter
Management system of borrowed books based on SSM framework
Win11如何开启移动热点?Win11开启移动热点的方法
C primer plus学习笔记 —— 2、常量与格式化IO(输入/输出)
PAT 乙等 1025 反转链表
三项最高级认证,两项创新技术、两大优秀案例,阿里云亮相云原生产业大会
Kotlin android简单Activity跳转、handler和thread简单配合使用
PAT 乙等 1023 组个最小数
新课上线 | 每次 5 分钟,轻松玩转阿里云容器服务!
Yingjixin ip5566 with type-C port 3A charging and discharging fast charging mobile power supply 5W wireless charging in one SOC
Raspberry pie assert preliminary exercise
What is the magic of digital collections? Which reliable teams are currently developing