227. 基本计算器 II

实现一个基本的计算器来计算一个简单的字符串表达式的值。

字符串表达式仅包含非负整数,+, - ,*,/ 四种运算符和空格 。 整数除法仅保留整数部分。

示例 1:

输入: “3+2*2”

输出: 7

示例 2:

输入: " 3/2 "

输出: 1

示例 3:

输入: " 3+5 / 2 "

输出: 5

说明:

你可以假设所给定的表达式都是有效的。

请不要使用内置的库函数 eval。

PS:

每次处理完一个数就压栈,可以不处理括号

class Solution {
public int calculate(String s) {
Stack<Integer> stack = new Stack<>(); int sign = 1; // 表示正负号 2-3*3 = 2+(-3)*3
int msign = 0; // 1表示相乘 -1 表示相除 0 表示无操作
for(int i=0; i<s.length(); i++){
char ch = s.charAt(i);
if(Character.isDigit(ch)){
// 如果是数字
int num = ch - '0';
while(i+1<s.length() && Character.isDigit(s.charAt(i+1))){
num = num*10 + (s.charAt(i+1) - '0');
i++;
} if(msign == 1){ //相乘
stack.push(stack.pop() * num);
msign = 0;
}else if(msign == -1){ // 相除
stack.push(stack.pop() / num);
msign = 0;
}else{
stack.push(num * sign);
} }else if(ch == '+'){
sign = 1;
}else if(ch == '-'){
sign = -1;
}else if(ch == '*'){
msign = 1;
sign = 1;
}else if(ch == '/'){
msign = -1;
sign = 1;
}
} int res = 0;
while(!stack.isEmpty()){
res += stack.pop();
}
return res;
}
}

Java实现 LeetCode 227 基本计算器 II(二)的更多相关文章

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

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

  2. Leetcode 227.基本计算器II

    基本计算器II 实现一个基本的计算器来计算一个简单的字符串表达式的值. 字符串表达式仅包含非负整数,+, - ,*,/ 四种运算符和空格  . 整数除法仅保留整数部分. 示例 1: 输入: " ...

  3. [LeetCode] 227. 基本计算器 II

    题目链接: https://leetcode-cn.com/problems/basic-calculator-ii 难度:中等 通过率:33.2% 题目描述: 实现一个基本的计算器来计算一个简单的字 ...

  4. Java for LeetCode 227 Basic Calculator II

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

  5. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  6. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  7. Java for LeetCode 059 Spiral Matrix II

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  8. Java for LeetCode 126 Word Ladder II 【HARD】

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

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

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

随机推荐

  1. BufferedInputStream:字节缓冲输入流

    package com.itheima.demo01.BufferedStream; import java.io.BufferedInputStream; import java.io.FileIn ...

  2. [codeforces-543-D div1]树型DP

    题意:给一棵树的边标上0或1,求以节点i为源点,其它点到i的唯一路径上的1的边数不超过1条的方案数,输出所有i的答案. 思路:令f[i]表示以节点i为源点,只考虑子树i时的方案数,ans[i]为最后答 ...

  3. Spring Boot 之 Spring Batch 批处理实践

    实践内容 从 MariaDB 一张表内读 10 万条记录,经处理后写到 MongoDB . 具体实现 1.新建 Spring Boot 应用,依赖如下: <!-- Web 应用 --> & ...

  4. 2018-06-19 js DOM对象

    DOM对象: Doucument Object Model即文档对象 DOM对象的操作: 1.找元素 返回元素对象: var obj=document.getElementById();//通过Id查 ...

  5. Lvs 调度算法

    lvs scheduler:仅根据IP和端口进行调度 静态方法:仅根据算法本身进行调度,不考虑当前服务器实际负载情况:保证起点公平 RR:round robin, 轮调,轮询,轮叫: 调度器通过&qu ...

  6. Jenkins-Sonar集成配置及注意点

    首先说说关于Jenkins集成Sonar的相关配置:我jenkins与Sonar不在同一个服务器上! 先现在 SonarQube Scanner 插件. SonarQube Servers:系统配置 ...

  7. 王艳 201771010127《面向对象程序设计(java)》第七周学习总结

    1.实验目的与要求 (1)进一步理解4个成员访问权限修饰符的用途: (2)掌握Object类的常用API用法: (3)掌握ArrayList类用法与常用API: (4)掌握枚举类使用方法: (5)结合 ...

  8. 为什么Tableviewcell创建时可以不判空

    dequeueReuseableCellWithIdentifier:与dequeueReuseableCellWithIdentifier:forIndexPath:的区别: 前者不必向tableV ...

  9. Fabric CA的部署与使用

    Fabric CA是Hyperledger Fbric的证书认证中心,提供以下功能:用户信息的登记与注册,数字证书的颁发与管理. 前言 之前使用CA服务一直是在docker容器中运行下载好的CA镜像, ...

  10. HashMap集合嵌套集合方法四种

    Map<String, HashMap<Person, String>> m=new HashMap<String, HashMap<Person, String& ...