当前位置:网站首页>Calculator (force buckle)
Calculator (force buckle)
2022-04-22 04:43:00 【Coaxed Conan】
title: Calculator
date: 2022-04-18 15:15:13
tags: Make a little progress every day
Daily topic
subject : Calculator
Given an integer containing a positive integer 、 Add (+)、 reduce (-)、 ride (*)、 except (/) Arithmetic expression of ( Except brackets ), Calculate the result .
The expression contains only non negative integers ,+, - ,*,/ Four operators and spaces . Division of integers preserves only the integral part .
Example :
Example 1:
Input : “3+2*2”
Output : 7Example 2:
Input : " 3/2 "
Output : 1Example 3:
Input : " 3+5 / 2 "
Output : 5explain :
You can assume that the given expressions are valid .
Please do not use built-in library functions eval.
Small pit , There are spaces in the string , Need to replace
Code :
class Solution {
public int calculate(String s) {
s =s.replaceAll(" ","");
Deque<Integer> stack = new ArrayDeque<>();
// First add the first one
char flag = '+';
// Initial value of the number
int num = 0;
for(int i=0;i<s.length();i++){
// Convert to numbers
if(Character.isDigit(s.charAt(i))){
num = num*10+s.charAt(i)-'0';
}
// Run according to the symbol , Namely * and /, High priority , First take out the numbers in the stack , Put it in after calculation
if(!Character.isDigit(s.charAt(i)) || i==s.length()-1){
if(flag=='+'){
stack.push(num);
}else if(flag=='-'){
stack.push(-num);
}else if(flag=='*'){
stack.push(stack.pop()*num);
}else if(flag=='/'){
stack.push(stack.pop()/num);
}
flag = s.charAt(i);
num =0;
}
}
// Traversal stack , Cumulative results
int res =0;
while(!stack.isEmpty()){
res += stack.pop();
}
return res;
}
}
That's all about the calculator
Copyright notice :
Original Blogger : Cowherd Conan
Blogger original link :https://keafmd.blog.csdn.net/
Personal blog links :https://www.keafmd.top/
If it helps you , Thank you for clicking on One key, three links Support !
[ ha-ha ][ Huai Quan ]


come on. !
Joint efforts !
Keafmd
You can see it here , You know the following , Let's make progress together !
版权声明
本文为[Coaxed Conan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210920598238.html
边栏推荐
- [untitled] timed
- 同行面试分享 联想 winform方向 20220420
- Solve the problem of Chinese garbled code in idea (garbled code in configuration file)
- 2022年A特种设备相关管理(电梯)复训题库及答案
- LeetCode 剑指 Offer 65. 不用加减乘除做加法***
- Leetcode refers to offer 18 Delete the node of the linked list
- Torch file saving and loading -- [torch learning notes]
- 目标检测--轻量级网络(截至2022-04-21)
- matlab如何实现不同的值显示不同的颜色
- LeetCode 剑指 Offer 43. 1~n 整数中 1 出现的次数***
猜你喜欢
![[experience] Why does the IP address of HP printer start with 169.254](/img/e7/5068a565b57f066377fcb5754bc4bd.png)
[experience] Why does the IP address of HP printer start with 169.254

Statistics of authoritative institutions: the best data center network company in 2021, China Huawei and H3C, were listed

Paper reading (48): a library of optimization algorithms for organizational design

如何在 Acrobat Pro DC 与其它文件合并创建单个 PDF 文件?

【板栗糖GIS】arcmap—如何制作带缓冲区的图幅结合表

5_ Data analysis - Data Visualization

创意几何点线面三角形孟菲斯海报banneer广告设计ai矢量背景素材

Kaggle Real Game 4.1 - Time Series Prediction Problem

Setting method of parameters such as color and linetype of MATLAB curve

10人小团队创业,如今收入过亿,不融资的简道云是怎么做到的?
随机推荐
AT32 MCU F435/437 DFU DEMO
Unity simple UI prefix tree red dot system
Peer interview sharing Lenovo WinForm direction 20220420
链表第四篇
2022 Shandong Province safety officer C certificate examination questions and online simulation examination
爬演员名字加链接
7_ Data analysis - Evaluation
[concurrent programming 046] for the synchronization method, how does the processor realize atomic operation?
The simulation generates random numbers and calculates the birthday of each student
15. Bufferevent client test connection server
14.buffferevent超时事件处理
Paper reading (49): big data security and privacy protection (Kopp)
Leetcode refers to offer 43 Number of occurrences of 1 in integers 1 ~ n***
如何在 Bash 中将字符串计算为数字?
Leetcode sword finger offer 15 Number of 1 in binary
pycharm+anaconda安装包
AT32 MCU Audio 24bit例程
2022g2 power station boiler stoker examination exercises and online simulation examination
Raspberry pie 4B compile paddlelite (detailed steps 2022)
2022p cylinder filling test question bank and simulation test