LeetCode——Basic Calculator
Description:
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.
用两个操作栈来处理,具体见代码。
public class Solution {
public static int calculate(String s) {
Stack<Integer> num = new Stack<>();
Stack<Character> op = new Stack<>();
op.push('+');
int n = s.length();
if(n < 1) return 0;
for(int i=0; i<n; ) {
if(s.charAt(i) == ' ') {
i ++;
}
else if(s.charAt(i) == '(') {
op.push('(');
op.push('+');
i ++;
}
else if(isOp(s.charAt(i))) {
op.push(s.charAt(i));
i ++;
}
//523 +-(++
else if(s.charAt(i) == ')') {
int res = 0;
while (!op.empty() && !(op.peek() == '(')) {
int temp = num.peek();
if (op.peek() == '-') temp = -temp;
res += temp;
num.pop();
op.pop();
}
System.out.println(res);
op.pop();
num.push(res);
i ++;
}
else {
int temp = 0;
while(i < n && isDigit(s.charAt(i))) {
temp = temp * 10;
temp += s.charAt(i) - '0';
i ++;
}
num.push(temp);
}
}
int res = 0;
while (!op.empty()) {
int temp = num.peek();
if (op.peek() == '-') temp = -temp;
res += temp;
num.pop();
op.pop();
}
return res;
}
public static boolean isOp(char c) {
if(c == '+' || c == '-') {
return true;
}
else {
return false;
}
}
public static boolean isDigit(char c) {
if(c >= '0' && c <='9') {
return true;
}
else {
return false;
}
}
}
LeetCode——Basic Calculator的更多相关文章
- [LeetCode] Basic Calculator II 基本计算器之二
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
- [LeetCode] Basic Calculator 基本计算器
Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...
- LeetCode Basic Calculator II
原题链接在这里:https://leetcode.com/problems/basic-calculator-ii/ Implement a basic calculator to evaluate ...
- LeetCode Basic Calculator
原题链接在这里:https://leetcode.com/problems/basic-calculator/ Implement a basic calculator to evaluate a s ...
- [LeetCode] Basic Calculator III 基本计算器之三
Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...
- [LeetCode] Basic Calculator IV 基本计算器之四
Given an expression such as expression = "e + 8 - a + 5" and an evaluation map such as {&q ...
- [LeetCode] Basic Calculator & Basic Calculator II
Basic Calculator Implement a basic calculator to evaluate a simple expression string. The expression ...
- LeetCode——Basic Calculator II
Description: Implement a basic calculator to evaluate a simple expression string. The expression str ...
- LeetCode() Basic Calculator 不知道哪里错了
class Solution {public: int calculate(string s) { stack<int> num; stack<ch ...
随机推荐
- 【 Linux 】单台服务器上并发TCP连接数
单台服务器上并发TCP连接数 问题:一台服务器到底能够支持多少TCP并发连接呢? 1. 文件描述符限制: 对于服务器来说,每一个TCP连接都要占用一个文件描述符,一旦文件描述符使用完,新的 ...
- SecureCRT 用ssh key登录配置方法
服务器端配置 OS: Debian-6.0.5 复制代码 代码如下: #apt-get install ssh 安装ssh服务 编辑/etc/ssh/sshd_config配置文件 复制代码 代码如下 ...
- jquery-ajax-php(内容点赞并进行cookie限制实现)
1.模板页html例如以下: 2.模板页的jquery里的ajax实现例如以下: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T ...
- Cocos2d-x 3.0final 终结者系列教程06-Director和场景跳转
这些天互联网大事不少呀 1.逻辑思维分家(所谓合久必分,分久必合,实属正常.切行切珍惜吧) 2. 锤子手机开卖 (无论你买没买,反正我没买,作为多年Android开发的我深知说的亮点事实上在我看来都 ...
- java-基于JavaMail的Java邮件发送
1.基于JavaMail的Java邮件发送:简单邮件发送 2.基于JavaMail的Java邮件发送:复杂邮件发送
- CSS 块级元素、内联元素概念
p.h1.或div等元素常常称为块级元素,这些元素显示为一块内容:Strong.span等元素称为行内元素,它们的内容显示在行中,即“行内框”.(可以使用display=block将行内元素转换成块元 ...
- ajax回调函数中使用$(this)取不到对象的解决方法
如果在ajax的回调函数内使用$(this)的话,实践证明,是取不到任何对象的,需要的朋友可以参考下 $(".derek").each(function(){ $(this).cl ...
- 一个极其高效的虚拟机内存冗余消除机制:UKSM
Linux内核机制KSM(Kernel Samepage Merging)能合并KVM虚拟机之间相同内存的页面,被CentOS, RHEL之类的服务器内核广泛采用,但是其速度很慢.UKSM(Ultra ...
- 象“[]”、“.”、“->”这类操作符前后不加空格
象“[]”.“.”.“->”这类操作符前后不加空格. #include <iostream> #include <process.h> #include<stdio ...
- Yii2框架加入API Modules
一.环境部署 1. read fucking Yii Documents. http://www.yiichina.com/doc/guide/2.0 2. 了解依赖注入模式 Java描写叙述: ht ...