Basic Calculator,Basic Calculator II
Implement a basic calculator to evaluate a simple expression string.
The expression string may contain open (
and closing parentheses )
, the plus +
or minus sign -
, non-negative integers and empty spaces .
You may assume that the given expression is always valid.
Some examples:
"1 + 1" = 2
" 2-1 + 2 " = 3
"(1+(4+5+2)-3)+(6+8)" = 23
Note: Do not use the eval
built-in library function.
/*
已知条件:
1.只包含空格,+,-,(,),非负整数
2.假设输入一定合法
测试用例
"6-(4-9)+7"
"8+(1+(4+5-(7-3)-2)-3)+(6+8)"
"1 + 1+2"
用括号分割表达式,遇到()先算括号表达式内容
*/
class Solution {
public:
int calculate(string &s ,int& start,int end)
{
char pre_op='+';
int num=,res=;
while(start<end){
if(s[start]==' '){
start++;continue;
}
if(isdigit(s[start])){
num = num*+(s[start++]-'');
}else if(s[start]=='('){
num = calculate(s,++start,end);
start++;
}else if(s[start]==')'){
return pre_op=='+' ? res+num:res-num;
}else{
res = pre_op=='+' ? res+num:res-num;
pre_op = s[start++];
num = ;
}
}
return pre_op=='+' ? res+num:res-num;
}
int calculate(string s) {
int start = ;
return calculate(s,start,s.size());
}
};
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.
/*
题目已知:
1.只包含非负整数,加减乘除,空格
2.假设输入一直合法
涉及的几个点:
1.数字分割
2.字符串转整数
3.运算符的优先级
可能隐藏的点:
大数
测试案例:
"0"
"1+0"
"1+10"
" 13 + 24 "
" 23+ 34*3 /2 "
" 12 - 7*3/2 + 35/7-3 "
" 7*2/3 + 9"
"12 - 7*3/2 + 35/7"
*/
class Solution {
public:
int calculate(string s) {
int size = s.size();
long long int exp_res = ,res=;
long int num =;
char pre_op='+';
for(int i=;i<size;i++){
if(s[i]==' ') continue;
if(isdigit(s[i])){
num = num*+(s[i]-'');
if(i+ == size || !isdigit(s[i+])){//如果下一个位置是最后一个位置,或者下一个位置不是数字了
if(pre_op=='+'){
exp_res = num;
}else if(pre_op=='-'){
exp_res = -num;
}else if(pre_op=='*'){
exp_res *= num;
}else{
exp_res /= num;
}
}
}else{
if(s[i]=='+' || s[i]=='-'){
res += exp_res;
}
pre_op = s[i];
num=;
}
}
return res+exp_res;
}
};
Basic Calculator,Basic Calculator II的更多相关文章
- [LeetCode] Basic Calculator & Basic Calculator II
Basic Calculator Implement a basic calculator to evaluate a simple expression string. The expression ...
- JavaPersistenceWithHibernate第二版笔记-第五章-Mapping value types-001Mapping basic properties(@Basic、@Access、access="noop"、@Formula、@ColumnTransformer、@Generated、 @ColumnDefaul、@Temporal、@Enumerated)
一.简介 在JPA中,默认所有属性都会persist,属性要属于以下3种情况,Hibernate在启动时会报错 1.java基本类型或包装类 2.有注解 @Embedded 3.有实现java.io. ...
- 胡喜:从 BASIC 到 basic ,蚂蚁金服技术要解决两个基本的计算问题
摘要: 揭开 BASIC College 神秘面纱,蚂蚁金服首次揭秘人才培养机制. 导读:5 月 6 日,蚂蚁金服副 CTO 胡喜在 2019 年 QCon 上做了<蚂蚁金服十五年技术架构演进之 ...
- Python Basic 01.Basic
01.variable ''' 변수(variable) - 자료(data)를 임시(휘발성) 저장하는 역할 - 실제 자료가 아닌 자료의 주소를 저장한다.(참조변수) ''' # 1. 변수 ...
- LeetCode Basic Calculator II
原题链接在这里:https://leetcode.com/problems/basic-calculator-ii/ Implement a basic calculator to evaluate ...
- [LeetCode] Basic Calculator IV 基本计算器之四
Given an expression such as expression = "e + 8 - a + 5" and an evaluation map such as {&q ...
- calculator
#include <stdio.h> #include <stdlib.h> typedef enum { FALSE = , TRUE }BOOL; void calcula ...
- Basic认证
Basic 概述 Basic 认证是HTTP 中非常简单的认证方式,因为简单,所以不是很安全,不过仍然非常常用. 当一个客户端向一个需要认证的HTTP服务器进行数据请求时,如果之前没有认证过,HTTP ...
- (Stack)Basic Calculator I && II
Basic Calculator I Implement a basic calculator to evaluate a simple expression string. The expressi ...
随机推荐
- Letter of application, e-mail version
Subject line: (logical to recipient!) Application for sales representative for mid-Atlantic area Apr ...
- Ribbon2: 创建动态的Ribbon库
Sam Radakovitz曾在Excel团队博客中发表过一篇文章,介绍了如何创建动态的Ribbon库,即如何通过RibbonX和VBA放置动态的图形图像到功能区库中,在该文中,作者创建了两个库:一个 ...
- Chrome和Firefox浏览器调试对比
最近的项目中使用Extjs5, 其中主要的一个特点就是js文件的动态加载,之前使用Firefox浏览器对js文件进行调试,打断点时,只对当次调试有效,刷新之后,由于动态加载的js文件(文件名后面加了一 ...
- ios9基础知识(技能篇)
NSFileManager.NSURL.NSFileHandle.NSData.NSXMLParser.NSUserDefaults.NSKeyedArchiver.NSKeyedUnarchiver ...
- iOS之极光推送
注册JPush账号 JPush官网 下载SDK 下载地址:https://www.jpush.cn 根据文档进行集成 文档:http://docs.jpush.io/guideline/ios_gui ...
- OC——网络解析获取图片的应用
headimageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, DEVW, DEVW/2)]; headimageView.cont ...
- UIView的clipsTobounds属性
这里的clip是修剪的意思,bounds是边界的意思,合起来就是:如果子视图的范围超出了父视图的边界,那么超出的部分就会被裁剪掉.该属性在实际工程中还是非常实用的,必须要了解清楚.
- Arcgis Engine 添加一个Symbol符号样式步骤
public static void DrawPictureMarkerSymbol(IGlobe globe, String layerName) { //添加一个图层 ESRI.ArcGIS.Ca ...
- php 数组 array_values () array_key()
<?php // array_unique($array) 去除重复 // array_unshif()向数组的顶部追加函数 // array_shif($a,"ss")向数 ...
- Python成长之路_装饰器
一.初入装饰器 1.首先呢我们有这么一段代码,这段代码假设是N个业务部门的函数 def f1(aaa): print('我是F1业务') if aaa == 'f1': return 'ok' def ...