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

题目:

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.

Example 1:

Input: "3+2*2"
Output: 7

Example 2:

Input: " 3/2 "
Output: 1

Example 3:

Input: " 3+5 / 2 "
Output: 5

Note:

  • You may assume that the given expression is always valid.
  • Do not use the eval built-in library function.

题解:

It could apply the generic method. Use two level of operations. Calculate * and / first, accumlate the result into num2.

Then + and -, accumlate the result into num1.

When current char is digit, get the current number, perform * and /.

If current char is * and /, update operator 2.

If current char is + and -, perform previous + and - operation and update operator 1, reset num2, operator 2.

Time Complexity: O(n). n = s.length().

Space: O(1).

AC Java:

 class Solution {
public int calculate(String s) {
if(s == null || s.length() == 0){
return 0;
} int num1 = 0;
int o1 = 1;
int num2 = 1;
int o2 = 1;
for(int i = 0; i<s.length(); i++){
char c = s.charAt(i);
if(Character.isDigit(c)){
int cur = c - '0';
while(i+1 < s.length() && Character.isDigit(s.charAt(i+1))){
cur = cur * 10 + s.charAt(i+1)-'0';
i++;
} num2 = o2 == 1 ? num2 * cur : num2 / cur;
}else if(c == '*' || c == '/'){
o2 = c == '*' ? 1 : -1;
}else if(c == '+' || c == '-'){
num1 = num1 + o1 * num2;
o1 = c == '+' ? 1 : -1; num2 = 1;
o2 = 1;
}
} return num1 + o1 * num2;
}
}

类似Basic CalculatorBasic Calculator III.

LeetCode Basic Calculator II的更多相关文章

  1. [LeetCode] Basic Calculator II 基本计算器之二

    Implement a basic calculator to evaluate a simple expression string. The expression string contains ...

  2. LeetCode——Basic Calculator II

    Description: Implement a basic calculator to evaluate a simple expression string. The expression str ...

  3. LeetCode 227. 基本计算器 II(Basic Calculator II)

    227. 基本计算器 II 227. Basic Calculator II 题目描述 实现一个基本的计算器来计算一个简单的字符串表达式的值. 字符串表达式仅包含非负整数,+,-,*,/ 四种运算符和 ...

  4. 【LeetCode】227. Basic Calculator II 解题报告(Python)

    [LeetCode]227. Basic Calculator II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...

  5. leetcode 224. Basic Calculator 、227. Basic Calculator II

    这种题都要设置一个符号位的变量 224. Basic Calculator 设置数值和符号两个变量,遇到左括号将数值和符号加进栈中 class Solution { public: int calcu ...

  6. 【LeetCode】227. Basic Calculator II

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

  7. [LeetCode] Basic Calculator & Basic Calculator II

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

  8. LeetCode OJ Basic Calculator II

    Basic Calculator II 题目 思路 和这个一样:Basic Calculator I 代码 class ExpressionTransformation { public: strin ...

  9. [LeetCode] Basic Calculator 基本计算器

    Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...

随机推荐

  1. Javascript刷新页面大全

    非模态刷新父页面:window.opener.location.reload(); 模态刷新父页面:window.dialogArguments.location.reload(); 先来看一个简单的 ...

  2. 将textField编辑完内容作为参数发送请求

    将textField编辑完内容作为参数发送请求  首先赋值默认值  其次把编辑完的内容传给model,这样的话,model里面的数据就是编辑完之后的内容了

  3. Scala - Spark Lambda“goesto“ => 分析

    /// 定义一个函数AddNoise,参数分别为rdd,Fraction.其中rdd为(BreezeDenseMatrix, BreezeDenseMatrix)元组构成的RDD.Fraction为一 ...

  4. hdu Super Jumping

    简单的dp,最优子结构是dp[i],即从0~i来看,是的dp[i]最大,然后找到最大中的最大就可以了, 转移方程是:dp[i]=max{dp[i],dp[j]+value[i]},注意这里有两个判断条 ...

  5. Servlet 编程 http请求类型

    HTTP协议的8种请求类型介绍 HTTP协议中共定义了八种方法或者叫“动作”来表明对Request-URI指定的资源的不同操作方式,具体介绍如下: OPTIONS:返回服务器针对特定资源所支持的HTT ...

  6. NP

    一个决定性问题C 若是为NPC,则代表它对NP是完备的,这表示: 它是一个NP问题,且 其他属于NP的问题都可归约成它. 满足条件2(无论是否满足条件1)的问题集合被称为NP-hard.一个NP-ha ...

  7. POJ 2892 Tunnel Warfare(线段树单点更新区间合并)

    Tunnel Warfare Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 7876   Accepted: 3259 D ...

  8. 【ArcGis for javascript从零开始】之一 ArcGis加载天地图

    最近做项目需要用到ArcGis来进行数据展示和数据分析.以前从来没有接触过与Gis有关的东西,一切需要从头开始学.没有时间从头系统地学习了,只能用到哪个学习哪里了,本系列只是对学习的路径进行记录.Ar ...

  9. jQuery如何去判断页面是否有父页面?

    jQuery如何去判断页面是否有父页面?     是要判断当前页面是否被嵌入在frame里吗? 1 2 3 if (top != self) {     alert('我在框架里'); }

  10. Windows中杀死占用某个端口的进程

    Windows中杀死占用某个端口的进程 netstat -ano | findstr //列出进程极其占用的端口,且包含 80 tasklist | findstr taskkill -PID < ...