(Stack)Basic Calculator I && II
Basic Calculator I
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.
参考leetcode的解法:
// "(1+(4+5+2)-3)+(6+8)" = 23
public static int calculate(String s) {
Stack<Integer> stack = new Stack<Integer>();
int sign = 1; //表示正负
int number = 0; //存储数值
int result = 0;
for(int i = 0; i<s.length(); i++){
char c = s.charAt(i);
switch (c) {
//遇到运算符时,把前面的数值带上其前面的运算符加到结果上
case '+':
result += sign * number;
number = 0;
sign = 1;
break;
case '-':
result += sign * number;
number = 0;
sign = -1;
break;
case '(': //c='('时相当于开始一个新表达式,把原来的结果和符号存入栈中
stack.push(result);
stack.push(sign);
result = 0;
sign = 1;
number = 0;
break;
case ')': //把当前的表达式结果运算完,加上符号累加到原来的结果
result += sign * number;
result *= stack.pop();
result += stack.pop();
number = 0;
break;
case ' ':
break;
default:
number = number * 10 + (c - '0');
break;
}
}
if(number != 0)
result += sign * number;
return result;
}
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.
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.
参考leetcode的解法:
// 1+2*3
public static int calculate(String s) {
Stack<Integer> stack = new Stack<Integer>();
char sign = '+';
int number = ;
int result = ;
for(int i=; i<s.length(); i++){
char c = s.charAt(i);
if(Character.isDigit(s.charAt(i))){ //读取数值
number = number * + c - '';
}
//当字符为运算符或者到达表达式末尾时
if(!Character.isDigit(c) && ' ' != c || i == s.length() - ){
switch (sign) { //判断前一个运算符,跟I的本质区别
case '+':
stack.push(number);
break;
case '-':
stack.push(-number);
break;
case '*': //当上一个运算符为*或者/,取出栈顶数进行运算后再放入栈中
stack.push(stack.pop()*number);
break;
case '/':
stack.push(stack.pop()/number);
break;
}
number = ;
sign = c;
}
}
for(Integer i : stack){
result += i;
}
return result;
}
(Stack)Basic Calculator I && II的更多相关文章
- Basic Calculator I && II && III
Basic Calculator I Implement a basic calculator to evaluate a simple expression string. The expressi ...
- leetcode 224. Basic Calculator 、227. Basic Calculator II
这种题都要设置一个符号位的变量 224. Basic Calculator 设置数值和符号两个变量,遇到左括号将数值和符号加进栈中 class Solution { public: int calcu ...
- 【LeetCode】227. Basic Calculator II
Basic Calculator II Implement a basic calculator to evaluate a simple expression string. The express ...
- [LeetCode] Basic Calculator & Basic Calculator II
Basic Calculator Implement a basic calculator to evaluate a simple expression string. The expression ...
- Basic Calculator - Stack(表达式计算器)
978. Basic Calculator https://www.lintcode.com/problem/basic-calculator/description public class Sol ...
- LeetCode OJ Basic Calculator II
Basic Calculator II 题目 思路 和这个一样:Basic Calculator I 代码 class ExpressionTransformation { public: strin ...
- LeetCode 227. 基本计算器 II(Basic Calculator II)
227. 基本计算器 II 227. Basic Calculator II 题目描述 实现一个基本的计算器来计算一个简单的字符串表达式的值. 字符串表达式仅包含非负整数,+,-,*,/ 四种运算符和 ...
- 【LeetCode】227. Basic Calculator II 解题报告(Python)
[LeetCode]227. Basic Calculator II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- [LeetCode] Basic Calculator II 基本计算器之二
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
随机推荐
- WPF Customize TabControl
有篇很好的文章 http://www.blogs.intuidev.com/post/2010/01/25/TabControlStyling_PartOne.aspx 详细介绍了如何Customiz ...
- [Qt] Mask 蒙版
[Qt] Mask 蒙版 Mask能够覆盖在其他的widget上面,实现一些动态图片的加载效果.下面给出代码. mask.h #ifndef MASK_HJ #define MASK_HJ #incl ...
- spring整合springMVC、mybatis、hibernate、mongodb框架
开发环境 eclipse Mars 4.5 JDK 1.7 框架 spring 4.0.5 mybatis 3.2.7 hibernate 4.3.6 mongodb 1.7 数据库 MySQL 5. ...
- AvalonEdit 移除自身ScrollViewer (可配合外部自定义ScrollViewer 使用)
http://community.sharpdevelop.net/forums/p/11977/42764.aspx#42764 1: <Style TargetType="{x:T ...
- lesson6:java线程中断
正常的情况下,业务系统都不会去中断它的线程,但是由于一些特殊情况的发生,线程已经不能正常结束了,并且此类线程已经影响到业务系统提供服务的能力,如果系统设计的健壮,便会通过监控线程去主动的中断此类线程. ...
- 浅析NSTimer & CADisplayLink内存泄露
偶得前言 NSRunLoop与定时器 - invalidate的作用 我们如何解决? 偶得前言 本篇文章中我们主要谈谈NSTimer\CADisplayLink在使用过程中牵扯到内存泄露的相关问题及解 ...
- 【BUG】Kewastunpackstats(): Bad Magic 1 (0x。。。。, 0)
Kewastunpackstats(): Bad Magic 1 (0x1108f7b87, 0) In Alert Logfile After Upgrading to 11.2.0.1 Appli ...
- C# 杨辉三角形算法
代码如下: static void Main(string[] args) { int[][] Array_int = new int[10][]; //向数组中记录杨辉三角形的值 for (int ...
- linux一键安装vncserver脚本
title: linux一键安装vncserver脚本 date: 2016-04-11 14:32:04 tags: --- linux多数情况下是作为服务器使用的,管理员一般也喜欢使用命令行来管理 ...
- compass的使用
compass常用的一些命令 compass create 创建新Compass项目 compass init 为已存在项目添加compass compass clean 移动已生成的文件和缓存 c ...