题目:

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

链接: http://leetcode.com/problems/basic-calculator/

题解:

刚开始以为要用和RPN一样的方法,其实不是的。可以one pass遍历整个数组并且得到结果。 需要使用一个栈来cache括号这种情况。

Time Complexity - O(n), Space Complexity - O(n)。

public class Solution {
public int calculate(String s) {
if(s == null || s.length() == 0)
return 0;
Stack<Integer> stack = new Stack<>();
int result = 0, curNum = 0, sign = 1; for(int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if(c == ' ')
continue;
else if(Character.isDigit(c))
curNum = curNum * 10 + (int)(c - '0');
else if (c == '+') {
result += sign * curNum;
curNum = 0;
sign = 1;
} else if (c == '-') {
result += sign * curNum;
curNum = 0;
sign = -1;
} else if (c == '(') {
stack.push(result);
stack.push(sign);
curNum = 0;
sign = 1;
result = 0;
} else if (c == ')') {
result += sign * curNum;
curNum = 0;
if(!stack.isEmpty())
result *= stack.pop();
if(!stack.isEmpty())
result += stack.pop();
}
} if(curNum != 0)
result += sign * curNum; return result;
}
}

Reference:

https://leetcode.com/discuss/39553/iterative-java-solution-with-stack

https://leetcode.com/discuss/41868/java-solution-stack

https://leetcode.com/discuss/39479/simple-c-in-24-ms

  

224. Basic Calculator的更多相关文章

  1. leetcode 224. Basic Calculator 、227. Basic Calculator II

    这种题都要设置一个符号位的变量 224. Basic Calculator 设置数值和符号两个变量,遇到左括号将数值和符号加进栈中 class Solution { public: int calcu ...

  2. 224. Basic Calculator + 227. Basic Calculator II

    ▶ 两个四则表达式运算的题目,第 770 题 Basic Calculator IV 带符号计算不会做 Orz,第 772 题 Basic Calculator III 要收费 Orz. ▶ 自己的全 ...

  3. Java for LeetCode 224 Basic Calculator

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

  4. (medium)LeetCode 224.Basic Calculator

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

  5. [leetcode]224. Basic Calculator

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

  6. 【LeetCode】224. Basic Calculator

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

  7. [LeetCode] 224. Basic Calculator 基本计算器

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

  8. [LC] 224. Basic Calculator

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

  9. 【LeetCode】224. Basic Calculator 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 参考资料 日期 题目地址:https://lee ...

随机推荐

  1. How to change comment

    AX2009 // USR Changed on 2013-07-10 at 12:57:46 by 7519 - Begin // USR Changed on 2013-07-10 at 12:5 ...

  2. 通过获取客户端Json数据字符串,反序列化为实体对象的一段代码

    #region 保存候选人数据 /// <summary> /// 保存候选人数据 /// </summary> /// <param name="entity ...

  3. Basic Vlan Concepts

    1.  Vlan Benefit ·To reduce CPU overhead on each device by reducing the number of devices that recei ...

  4. Zendframework 模块加载事件触发顺序。

    模块加载时事件触发的时间顺序: 0.loadModules(ModuleEvent::EVENT_LOAD_MODULES) 1.  loadModule.resolve(ModuleEvent::E ...

  5. yabeblog.me 关于Tomcat7部署 一台机器部署两个项目,一个用域名访问,一个用IP访问

    该内容来自 http://www.yabeBlog.me,转载请说明出处. 1.使用IP访问的项目放在Tomcat7 的webapps目录下面:比如:AAA 2.使用域名访问的项目放在Tomcat7的 ...

  6. ERROR 1005 (HY000): Can't create table'matrix.system_log' (errno: 150)

    CREATE TABLE `user` (`id` bigint(32) NOT NULL AUTO_INCREMENT ,`name` varchar(32) CHARACTER SET utf8 ...

  7. Seafile V4.1 安装笔记

    yum -y install gcc gcc-c++ make cmake pcre pcre-devel expat expat-devel curl wget mlocate gd gd-deve ...

  8. cocos2dx中的定时器及其分类

    cocos2dx中的定时器分三大类: 1.帧循环定时器 2.一次性定时器 3.自定义定时器 一.帧循环定时器,顾名思义,每一帧都会执行一次,用于实时性要求比较高的场合,如碰撞检测 void sched ...

  9. c#之反射总结

     1.了解什么事程序集 2.加载程序集 首先要加载需要加载的程序集,然后找到指定的类型,进而往下进行动态加载. 要加载的程序集中的内容: public class Class1:Person { pr ...

  10. LoadRunner 学习笔记(3)基础

    LoadRunner包含的组件: Virtual User Generator 录制最终用户业务流程并创建自动化性能测试脚本,即 Vuser 脚本 Controller 组织.驱动.管理并监控负载测试 ...