当前位置:网站首页>计算器(力扣)
计算器(力扣)
2022-04-21 09:21:00 【牛哄哄的柯南】
title: 计算器
date: 2022-04-18 15:15:13
tags: 每天进步一点点系列
每日题目
题目:计算器
给定一个包含正整数、加(+)、减(-)、乘(*)、除(/)的算数表达式(括号除外),计算其结果。
表达式仅包含非负整数,+, - ,*,/ 四种运算符和空格 。 整数除法仅保留整数部分。
示例:
示例 1:
输入: “3+2*2”
输出: 7示例 2:
输入: " 3/2 "
输出: 1示例 3:
输入: " 3+5 / 2 "
输出: 5说明:
你可以假设所给定的表达式都是有效的。
请不要使用内置的库函数 eval。
小坑,字符串里有空格,需要替换掉
代码:
class Solution {
public int calculate(String s) {
s =s.replaceAll(" ","");
Deque<Integer> stack = new ArrayDeque<>();
//先把第一个加进去
char flag = '+';
//数字的初始值
int num = 0;
for(int i=0;i<s.length();i++){
//转换成数字
if(Character.isDigit(s.charAt(i))){
num = num*10+s.charAt(i)-'0';
}
//根据符号进行运行,就是*和/,优先级高,先把栈中的数字拿出来,运算后放进去
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;
}
}
//遍历栈,累加结果
int res =0;
while(!stack.isEmpty()){
res += stack.pop();
}
return res;
}
}
以上就是计算器的全部内容
版权声明:
原创博主:牛哄哄的柯南
博主原文链接:https://keafmd.blog.csdn.net/
个人博客链接:https://www.keafmd.top/
看完如果对你有帮助,感谢点击下面的一键三连支持!
[哈哈][抱拳]


加油!
共同努力!
Keafmd
都看到这里了,下面的内容你懂得,让我们共同进步!
版权声明
本文为[牛哄哄的柯南]所创,转载请带上原文链接,感谢
https://keafmd.blog.csdn.net/article/details/124250737
边栏推荐
- 深蓝-视觉slam-第六节习题
- Is annuity insurance safe and reliable? How much is the profit?
- Binary tree knowledge
- Actual combat penetration - fofa dirbrute - code audit - construction POC ueditor - decryption - WAF Godzilla
- 1169: 大整数(指针专题)
- Yapi basic use (2022-04-15)
- Buuctf [geek challenge 2019] easysql
- 极客大挑战 2019 Upload 1
- 1161: string length (pointer)
- Getting started with object detection FAQs (deep learning / image classification)
猜你喜欢

CC00019.CloudJenkins—————————————

Actual combat penetration - fofa dirbrute - code audit - construction POC ueditor - decryption - WAF Godzilla

PageRank case Airport

编程如何提高自己的水平能力?学编程最重要的是什么?请看凡人浅谈如何学C
纯c语言链表实现学生信息管理系统.(你学会了吗?)

Pipy MQTT 代理之(三)Logging
![BUUCTF[ACTF2020 新生赛]Include](/img/20/3c0d0286c869385ee0c9a119e337e2.png)
BUUCTF[ACTF2020 新生赛]Include

2022 t elevator repair test questions and online simulation test
![BUUCTF[极客大挑战 2019]EasySQL](/img/35/84422c6aea0c96f6cbcf8354729604.png)
BUUCTF[极客大挑战 2019]EasySQL

CC00026.CloudJenkins—————————————
随机推荐
1146: 吃糖果
Add log4j log function
Garbage collection mechanism
Maya basic tutorial and basic operation explanation
Pyinstaller package exe (detailed tutorial)
1164: 字符串加密
2022 Shandong Province safety officer C certificate examination questions and simulation examination
Actual combat penetration - fofa dirbrute - code audit - construction POC ueditor - decryption - WAF Godzilla
BUUCTF[极客大挑战 2019]Havefun
Characteristics of interactive multimedia applications
[MySQL] Based on linux-centos7 9 detailed installation tutorial
Drafting and Revision: Laplacian Pyramid Network for Fast High-Quality Artistic Style Transfer--T Li
2022 mobile crane driver examination exercises simulated examination platform operation
CC00019. CloudJenkins—————————————
PyS1:概述
pyinstaller打包exe(详细教程)
Grid division [DFS]
【ACM】131. 分割回文串
头大毕设论文模板适配指南
1153: the longest sequence of simple version