当前位置:网站首页>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 !
边栏推荐
- Recommendation letter brain correspondent: if love is just a chemical reaction, can you still believe in love?
- Work study management system based on ASP
- 【空间&单细胞组学】第1期:单细胞结合空间转录组研究PDAC肿瘤微环境
- 2022年焊工(技师)考试题库模拟考试平台操作
- Four visualization tools are recommended to solve 99% of large screen visualization projects!
- Leetcode (406) - rebuild the queue based on height
- Construction and management practice of ByteDance buried point data flow
- What are the consequences of opening an account with Huatai Securities? How to open an account is the safest
- 请问一下,是不是insert all这种oracle的批量新增没拦截?
- 从小小线虫谈起——溯源神经系统进化,开启生命模拟
猜你喜欢
A bug liver a week I can't help mentioning issue
Construction and management practice of ByteDance buried point data flow
【中移芯昇】5. spi接口测试tf卡
外贸SEO 站长工具
dolphinscheduler2. Installation of X (valid for personal test)
3. Caller 服务调用 - dapr
美因基因港交所上市:市值43亿港元 IPO被市场忽略
基于ASP的勤工俭学管理系统
The time schedule for the soft test in the second half of 2022 has been determined!
中国内地仅四家突围 联想智慧颐和园荣获 “2022年IDC亚太区智慧城市大奖”
随机推荐
老板囑咐了三遍:低調、低調、低調
Why can't Bert completely kill the BM25??
机器人的运动范围(DFS)
Jingyuan's safe sprint to the Growth Enterprise Market: it plans to raise 400million yuan for investment and Yunyou software is the shareholder
开闭原则
2022 recurrent training question bank and online simulation examination for safety inspection of metal and nonmetal mines (underground mines)
【mysql学习笔记23】索引优化
dolphinscheduler2.X的安装(亲测有效)
PMP认证证书的续证费用是多少?
open3d里pointcloud和numpy数组之间的转化
Which securities company is the largest and safest? How to open an account is the safest
New drug discovery methods, AstraZeneca team improves ab initio molecular design through course learning
运行近20年,基于Win 98的火星探测器软件迎来首次升级
The time schedule for the soft test in the second half of 2022 has been determined!
基于ASP的勤工俭学管理系统
荐书丨《大脑通信员》:如果爱情只是化学反应,那还能相信爱情吗?
Leetcode(406)——根据身高重建队列
openGauss内核:SQL解析过程分析
Youju new material rushes to Shenzhen Stock Exchange: it plans to raise 650million yuan, with an annual revenue of 333million yuan
Conversion between pointcloud and numpy arrays in open3d