一.Basic Calculator
Total Accepted: 18480 Total Submissions: 94750 Difficulty: Medium

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());
}
};
 
二.Basic CalculatorII
Total Accepted: 14291 Total Submissions: 64507 Difficulty: Medium

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的更多相关文章

  1. [LeetCode] Basic Calculator & Basic Calculator II

    Basic Calculator Implement a basic calculator to evaluate a simple expression string. The expression ...

  2. 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. ...

  3. 胡喜:从 BASIC 到 basic ,蚂蚁金服技术要解决两个基本的计算问题

    摘要: 揭开 BASIC College 神秘面纱,蚂蚁金服首次揭秘人才培养机制. 导读:5 月 6 日,蚂蚁金服副 CTO 胡喜在 2019 年 QCon 上做了<蚂蚁金服十五年技术架构演进之 ...

  4. Python Basic 01.Basic

    01.variable ''' 변수(variable) - 자료(data)를 임시(휘발성) 저장하는 역할 - 실제 자료가 아닌 자료의 주소를 저장한다.(참조변수) ''' # 1. 변수 ...

  5. LeetCode Basic Calculator II

    原题链接在这里:https://leetcode.com/problems/basic-calculator-ii/ Implement a basic calculator to evaluate ...

  6. [LeetCode] Basic Calculator IV 基本计算器之四

    Given an expression such as expression = "e + 8 - a + 5" and an evaluation map such as {&q ...

  7. calculator

    #include <stdio.h> #include <stdlib.h> typedef enum { FALSE = , TRUE }BOOL; void calcula ...

  8. Basic认证

    Basic 概述 Basic 认证是HTTP 中非常简单的认证方式,因为简单,所以不是很安全,不过仍然非常常用. 当一个客户端向一个需要认证的HTTP服务器进行数据请求时,如果之前没有认证过,HTTP ...

  9. (Stack)Basic Calculator I && II

    Basic Calculator I Implement a basic calculator to evaluate a simple expression string. The expressi ...

随机推荐

  1. 使用一个HttpModule拦截Http请求,来检测页面刷新(F5或正常的请求)

    在Web Application中,有个问题就是:“我怎么来判断一个http请求到底是通过按F5刷新的请求还是正常的提交请求?” 相信了解ASP.NET的人知道我在说什么,会有同感,而且这其实不是一个 ...

  2. Jumpserver

    Jumpserver 是一款由python编写开源的跳板机(堡垒机)系统,实现了跳板机应有的功能.基于ssh协议来管理,客户端无需安装agent. 支持常见系统: redhat centos debi ...

  3. jQuery Validate 插件验证,,返回不同信息(json remote)自定义

    问题 申请账号需要确认该账号是存在 jquery.validate.js中的remote Jquery Ajax获取后台返回的Json数据后,添加自定义校验 解题思路:输入的登陆信息远程验证是否该账号 ...

  4. apache AH01630: client denied by server configuration错误解决方法

    今天本来是想要在自己本地搭建一个wamp环境用来做一些代码的测试和框架的学习. 鉴于目前工作的时候用到了php5.5,所以就用了wamp-server V2.5版本,安装完成之后配置虚拟主机一直出现4 ...

  5. python 的内建函数

    lambda 函数:lambda语句中,冒号前是参数,可以有多个,用逗号隔开,冒号右边的返回值 1. map/reduce 函数 (1)map()函数接收两个参数,一个是函数,一个是序列,map将传入 ...

  6. 匈牙利算法(素数伴侣(HW1112))

    #define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<vector> #include<string&g ...

  7. 自动生成XML空节点格式的差异

    我们用C#开发了上位机配置软件,用C开发了嵌入式软件,然后他们之间的参数交互靠XML文件来沟通. C#中添加一个空的节点有以下几种情况. 不给节点的InnerText赋值: <root> ...

  8. windows程序设计读书笔记2——字符显示1

    本程序使用GetSystemMetrics获取windows各种图像选项,并输出字符到窗口中. #define WINVER 0x0500 #include <windows.h> #in ...

  9. Mfgtool

    For bootstrap mode, it refers to the communcation between the host and ROM codes through serial down ...

  10. linux scp ssh命令不用输入密码

    把你的本地主机用户的ssh公匙文件复制到远程主机用户的~/.ssh/authorized_keys文件中 假设本地主机linux100,远程主机linux200 一,在linux100主机里的用户 运 ...