Java实现 LeetCode 227 基本计算器 II(二)
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(二)的更多相关文章
- LeetCode 227. 基本计算器 II(Basic Calculator II)
227. 基本计算器 II 227. Basic Calculator II 题目描述 实现一个基本的计算器来计算一个简单的字符串表达式的值. 字符串表达式仅包含非负整数,+,-,*,/ 四种运算符和 ...
- Leetcode 227.基本计算器II
基本计算器II 实现一个基本的计算器来计算一个简单的字符串表达式的值. 字符串表达式仅包含非负整数,+, - ,*,/ 四种运算符和空格 . 整数除法仅保留整数部分. 示例 1: 输入: " ...
- [LeetCode] 227. 基本计算器 II
题目链接: https://leetcode-cn.com/problems/basic-calculator-ii 难度:中等 通过率:33.2% 题目描述: 实现一个基本的计算器来计算一个简单的字 ...
- Java for LeetCode 227 Basic Calculator II
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
- 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 ...
- 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 ...
- 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 ...
- Java for LeetCode 126 Word Ladder II 【HARD】
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- [LeetCode] 227. Basic Calculator II 基本计算器之二
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
随机推荐
- 一文搞懂HMM(隐马尔可夫模型)-转载
写在文前:原博文地址:https://www.cnblogs.com/skyme/p/4651331.html 什么是熵(Entropy) 简单来说,熵是表示物质系统状态的一种度量,用它老表征系统的无 ...
- uCOS2014.1.8
目前uCOS中已经接触到的全局变量: OSTCBCur OSIntNesting OSPrioHighRdy 最高优先级任务 任哲编著<嵌入式实时操作系统uC/OS-II原理及应用> ...
- python语法学习第十天--类与对象相关的BIF、魔法方法
一些相关的BIF: issubclass(class,classInfo)#判断是否为子类,classInfo可以为多个类的元组,其中一个是,返回true,一个类也被认为是自己的子类,object是所 ...
- 最近常问的99道Java多线程面试题 !
前言 今天给大家更新的是一篇关于多线程面试的文章,也是霸哥根据时下热门的面试内容给大家进行总结的, 本篇文章属于干货内容! 请各位读者朋友一定要坚持读到最后,完整阅读本文后相信你对多线程会有不一样感悟 ...
- UEFI Shell --常用命令解释
UEFI Shell解释 UEFI Shell 是一个提供用户和UEFI系统之间的接口,进入UEFI Shell可以对计算机系统进行配置 命令解释: 单独的help就可以输出所有指令,不做特殊说明,内 ...
- hdu4035 Maze 题解
/* 设 E[i]表示在结点i处,要走出迷宫所要走的边数的期望. E[i] = ki*E[1] + (1-ki-ei)*E[fa[i]] + (1-ki-ei); E[i] = ki*E[1] + ( ...
- Vue中import用法
1. 引入第三方插件 第三方常用插件参考https://blog.csdn.net/vbirdbest/article/details/86527886 2. 导入 css 文件 import 'iv ...
- Interactive and non-interactive shell环境变量的差异
背景 在mac上安装mosh server后,用mosh client去连接,一直报command not found的错 zsh: command not found: mosh-server 但是 ...
- 7.1 Go interface
7.1 Go interface 雨痕-Go语言笔记 接口采用了duck type方式,在程序设计中是动态类型的一种风格 `当看到一只鸟走起来像鸭子.游泳起来像鸭子.叫起来也像鸭子,那么这只鸟就可以被 ...
- Spring 中的事件处理
Spring 中的事件处理 Spring 的核心是 ApplicationContext,它负责管理 beans 的完整生命周期.当加载 beans 时,ApplicationContext 发布某些 ...