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的更多相关文章

  1. [LeetCode] Basic Calculator II 基本计算器之二

    Implement a basic calculator to evaluate a simple expression string. The expression string contains ...

  2. [LeetCode] Basic Calculator 基本计算器

    Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...

  3. LeetCode Basic Calculator II

    原题链接在这里:https://leetcode.com/problems/basic-calculator-ii/ Implement a basic calculator to evaluate ...

  4. LeetCode Basic Calculator

    原题链接在这里:https://leetcode.com/problems/basic-calculator/ Implement a basic calculator to evaluate a s ...

  5. [LeetCode] Basic Calculator III 基本计算器之三

    Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...

  6. [LeetCode] Basic Calculator IV 基本计算器之四

    Given an expression such as expression = "e + 8 - a + 5" and an evaluation map such as {&q ...

  7. [LeetCode] Basic Calculator & Basic Calculator II

    Basic Calculator Implement a basic calculator to evaluate a simple expression string. The expression ...

  8. LeetCode——Basic Calculator II

    Description: Implement a basic calculator to evaluate a simple expression string. The expression str ...

  9. LeetCode() Basic Calculator 不知道哪里错了

    class Solution {public:    int calculate(string s) {        stack<int> num;        stack<ch ...

随机推荐

  1. HTML——表单与锚点

    练习做一个邮箱的注册页面 1.12行2列的表格 2.表格里面嵌入表单 3.最后建一个锚点 4.写具体内容 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTM ...

  2. ansible unarchive模块

    unarchive模块:http://docs.ansible.com/ansible/unarchive_module.html 功能:解压缩,这个模块有两种用法: 1.将ansible主机上的压缩 ...

  3. 监控http服务脚本

    [root@ob1 scripts]# vim test_httpd.sh 1 #!/bin/bash 2 #ss -tlnup|grep :80 >/dev/null 2>&1 ...

  4. tp-03 模板显示

    方式模板:$this->display(); 每当建立一个控制器  都要在view建立一个名字相对应的文件夹   在建立相对于的页面.

  5. jQuery实现点击单选按钮切换选中状态效果

    实现效果:第一次点击单选按钮时选中该按钮,再次点击时取消选中该单选按钮. 关键就是要将单选按钮原来的值保存起来,第二次点击时.如果原来选中则取消,否则选中. 看代码吧,是用jQuery实现的,功能虽小 ...

  6. Python 变量类型和运算符

    -*- coding:utf-8 -*- ''' if语法 if conditon: [tab键] command [tab键] command ... else: [tab键] command [t ...

  7. C++对析构函数的误解

    C++析构前言 析构函数在什么时候会自动被调用,在什么时候需要手动来调用,真不好意思说偶学过C++…今日特此拨乱反正. C++析构误解正文 对象在构造的时候系统会分配内存资源,对一些数据成员进行初始化 ...

  8. TensorFlow基础笔记(11) conv2D函数

    #链接:http://www.jianshu.com/p/a70c1d931395 import tensorflow as tf import tensorflow.contrib.slim as ...

  9. 静态库lib

    步骤: 按普通方式编程,无需export 其中静态库就是中间文件,跟obj文件类似. 静态库的使用不太方便: 如果该静态库是vs2008编译的,那么APP也得用vs2008编译,版本必须一致.且编译方 ...

  10. getaddrinfo ENOTFOUND https://api.weixin.qq.com https://api.weixin.qq.com:443

    原因:这是由于你当前的主机不能够连接到你填写的url. 解决方法: 先ping api.weixin.qq.com ping不通的话,就是外网访问的问题. 开通外网访问就可以了.