(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 ...
随机推荐
- (转)20 个大大节省你时间的 HTML5 开发工具
Rendera 如果你希望有个环境可以测试.浏览和体验各种不同的 CSS/HTML 和 JavaScript 代码,Rendera 为你提供了实时的运行结果.类似 RunJS. Patternizer ...
- php增删改查,自己写的demo
1.链接数据库通用方法:conn.php <?php //第一步:链接数据库 $conn=@mysql_connect("localhost:3306","root ...
- Android应用调试经常使用知识
1.Android应用启动过程调试 1).进入设置-->辅助功能-->开发人员选项:假设没有打开开发人员模式.在拨号里面输入*#*#6961#*#*: 2).找到选择调试应用,打开选择你要 ...
- IE下判断IE版本语法使用
先摆一下判断IE版本语法 <!--[if lte IE 6]> <![endif]--> IE6及其以下版本可见 <!--[if lte IE 7]> <![ ...
- 酷Q机器人,QQ机器人使用教程
软件介绍: 酷Q,软件酷Q机器人是一款基于webqq开发的一款自动接收.处理qq消息的软件. 改程序使用易语言编写,精简大量不必要代码,减小了软件体积,优化程序速度,使得酷Q更加轻巧好用. 在消息处理 ...
- Linux下的进程控制块——task_struct
在Linux中具体实现PCB的是 task_struct数据结构,以下实现摘自github 我想说它真的很长很长...... ↓ struct task_struct { volatile long ...
- mysql主从监控
要求:检测myslq从库状态,跳过固定的错误号,每隔30秒检测一次,如果符合条件自动跳过或者是重启从库 1)取出mysql从库的关键字 [root@localhost scripts]# mysql ...
- 配置NFS服务
1. NFS配置,需要安装哪些包?nfs-utils 和 rpcbind2. 如果不开启rpcbind服务,就启动NFS,会怎么样?如果不开启rpcbind服务,会报错:rpc.nfsd: writ ...
- django: template variable
模板变量用双大括号显示,如: <title>page title: {{title}}</title> 一 模板中使用变量 继续前面的例子,修改 index.html: < ...
- C# Dictionary 应用
1.字典定义并添加数据 Dictionary<string, string> dic = new Dictionary<string, string>(); dic.Add(& ...