当前位置:网站首页>Calculator (force buckle)
Calculator (force buckle)
2022-06-28 14:44:00 【Hua Weiyun】
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
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 !
边栏推荐
- Leetcode (88) -- merge two ordered arrays
- [MySQL learning notes 24] index design principles
- G: maximum flow problem
- Thread life cycle and its methods
- What is the renewal fee for PMP certificate?
- 基于 Nebula Graph 构建百亿关系知识图谱实践
- 证券公司和银行哪个更安全 怎么办理开户最安全
- 股票开户优惠链接,我如何才能得到?手机开户是安全么?
- Leetcode(167)——两数之和 II - 输入有序数组
- vector详解+题目
猜你喜欢

Vscode writes markdown file and generates pdf

What is the renewal fee for PMP certificate?

Performance comparison of deep learning models on cat and dog image data sets

Open source invites you to participate in openinfra days China 2022. Topic collection is in progress ~

Talking from the little nematode -- tracing the evolution of nervous system and starting life simulation

A bug liver a week I can't help mentioning issue

Construction and management practice of ByteDance buried point data flow

Ionq and Ge research confirmed that quantum computing has great potential in risk aggregation

Adding virtual environments to the Jupiter notebook

Based on asp Net based document retrieval system
随机推荐
优巨新材冲刺深交所:拟募资6.5亿 年营收3.33亿
vector详解+题目
[MySQL learning notes 23] index optimization
How to count dimensions of foreign trade E-mail Promotion
Technical trendsetter
324. swinging sort II: not a simple construction problem
Youju new material rushes to Shenzhen Stock Exchange: it plans to raise 650million yuan, with an annual revenue of 333million yuan
竞远安全冲刺创业板:拟募资4亿 启元投资与云游软件是股东
Recommended practice sharing of Zhilian recruitment based on Nebula graph
猫狗图像数据集上的深度学习模型性能对比
G: maximum flow problem
2022 Chinese cook (Advanced) test questions and online simulation test
[MySQL learning notes 24] index design principles
Talking from the little nematode -- tracing the evolution of nervous system and starting life simulation
Dry goods | how to calculate the KPI of scientific researchers, and what are the h index and G index
Mulan open work license 1.0 open to the public for comments
Seata数据库中出现以下问题要怎么解决呀?
Do not use short circuit logic to write STL sorter multi condition comparison
js 判断字符串为空或者不为空
Thread life cycle and its methods