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 ...
随机推荐
- jquery 判断多组radio checkbox是否选中
最近要做一个问卷调查的小页面,需要判断用户是否每项都有选择,如果每个都挨个判断很苦逼,所以网上搜了搜,自己也总结了一下,写了一段小代码~哈哈,水平有限大家见谅.html代码就不上了,N多单选和多选框就 ...
- mysql+php+pdo批量添加大数据
1.使用insert into插入 ini_set('max_execution_time','0');//限制超时时间,因为第一种时间较长,索性设为0不限制 $pdo = new PDO(" ...
- wamp出现问题#1045 - Access denied for user 'root'@'localhost' (using password: NO)的解决方法
打开wamp->apps->phpmyadmin目录下面的config.inc.php文件 cfg['Servers'][$i]['verbose'] = 'localhost';$cfg ...
- 在Javascript中使用String.startsWith和endsWith
在Javascript中使用String.startsWith和endsWith 在操作字符串(String)类型的时候,startsWith(anotherString)和endsWith(anot ...
- android studio error configuration with name default not found
Android Studio报错: android studio error configuration with name default not found 在进行sync的时候,提示Error: ...
- ORA-04092: COMMIT 不能在触发器中
触发器无需commit也不能写commit触发器和触发它的DML是同一个事务DML提交了,触发器的操作也提交了,要不就一起回滚了 当然,如果你一定要在触发器里写COMMIT那就用自治事务相当于一个事务 ...
- eclipse tomcat内存设置
-Xms256M -Xmx512M -XX:PermSize=256m -XX:MaxPermSize=512m
- Word Amalgamation(hdoj1113)
Word Amalgamation Problem Description In millions of newspapers across the United States there is a ...
- Nginx Upload Module 上传模块
传统站点在处理文件上传请求时,普遍使用后端编程语言处理,如:Java.PHP.Python.Ruby等.今天给大家介绍Nginx的一个模块,Upload Module上传模块,此模块的原理是先把用户上 ...
- Android学习笔记——Activity的启动和创建
http://www.cnblogs.com/bastard/archive/2012/04/07/2436262.html Android Activity学习笔记——Activity的启动和创建 ...