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. Vulnhb 靶场系列:Jarbas1.0

    靶场镜像 官网 信息收集 攻击机kali IP地址 通过nmap 进行主机发现,发现目标机IP地址 nmap -sP 192.168.227.1/24 参数说明: -sP (Ping扫描) 该选项告诉 ...

  2. Windows系统目录

    文件功能 编辑 ├—WINDOWS │ ├—system32(存放Windows的系统文件和硬件驱动程序) │ │ ├—config(用户配置信息和密码信息) │ │ │ └—systemprofil ...

  3. Unity直接调用Android Toast

    Unity直接调用Android Toast 这两天在搭一套UI框架,想把Android的Toast直接集成上去,有不想直接打jar包,所有写了个C#直接调用,废话不多说,直接干货: using Un ...

  4. Linux shell 正则表达式用法

    1.“ \  ” 用法 用于关闭其后续字符的特殊含义,恢复字符的本身含义,如:\\ 表示字符 \ 2. “ . " 用法 匹配任意单个字符 3. " * " 用法 匹配任 ...

  5. Linux相关命令、虚拟机网络配置

    虚拟机联网 Linux命令 1.查找 #查找django进程,不包括grep自建的 ps -ef |grep django | grep -v grep # find 查找home目录下的name.t ...

  6. 1.2Go环境搭建之Mac

    1.下载mac版go开发工具包,源码包或是安装包都可以 //官方下载地址 https://golang.org/dl/ //下载地址在此 https://dl.google.com/go/go1.11 ...

  7. PG primary 和 slave 互换

    http://blog.sina.com.cn/s/blog_544a710b0101a122.html http://blog.51cto.com/heyiyi/1898506 https://bl ...

  8. chosen.jquery.min.js select2.js 弊端

    chosen.jquery.min.js --将select放在页面最下方,会导致页面高度增加,最下方空白多出来 select2.js --点击select 但未选择,然后移出鼠标,发现其他文本框.关 ...

  9. python-基站位置查询

    本文采用的接口是聚合数据提供的 python2.7环境,3.x环境试了下好像不支持,获取位置信息为空 如下为运用代码: #coding=utf-8 ''' Created on 2019年9月18日 ...

  10. xshell使用技巧

    XShell是一款Windows下的一款远程连接Linux主机的工具,类似的软件还有SecureCRT,putty等,但是个人感觉XShell好用,功能强大.. 一.复制和粘贴 linux的Shell ...