实现一个基本的计算器来计算一个简单的字符串表达式。

字符串表达式可以包含左括号 ( ,右括号),加号+ ,减号 -,非负整数和空格 。

假定所给的表达式语句总是正确有效的。

例如:

"1 + 1" = 2
" 2-1 + 2 " = 3
"(1+(4+5+2)-3)+(6+8)" = 23
注意:不要使用内置的库函数 eval。

详见:https://leetcode.com/problems/basic-calculator/description/

Java实现:

1.遇到数字位,看后一位是否为数字,若是位数字,当前位需要进十.
2.开始设sign = 1,若遇到 ' - ', sign 改为 -1,若遇到 '+',sign改回1.
3.遇到 '(', 压栈,先压之前的res,后压sign,然后初始化res和sign.
4.遇到')' ,出栈,当前res先乘pop() 出来的 sign,再加上pop()出来的之前结果.

class Solution {
public int calculate(String s) {
if(s == null || s.length() == 0){
return -1;
}
int res = 0;
int sign = 1;
Stack<Integer> stk = new Stack<Integer>();
for(int i = 0; i<s.length(); i++){
char c = s.charAt(i);
if(Character.isDigit(c)){
int cur = c-'0';
while(i+1 < s.length() && Character.isDigit(s.charAt(i+1))){
cur = cur*10 + s.charAt(i+1) - '0';
++i;
}
res += sign*cur;
}else if(c == '-'){
sign = -1;
}else if(c == '+'){
sign = 1;
}else if(c == '('){
stk.push(res);
res = 0;
stk.push(sign);
sign = 1;
}else if(c == ')'){
res = res*stk.pop() + stk.pop();
}
}
return res;
}
}

参考:https://www.cnblogs.com/Dylan-Java-NYC/p/4825019.html

C++实现:

class Solution {
public:
int calculate(string s) {
int size=s.size();
if(size==0||s.empty())
{
return 0;
}
int res=0,sign=1;
stack<int> stk;
for(int i=0;i<size;++i)
{
char c=s[i];
if(c>='0')
{
int num=0;
while(i<size&&s[i]>='0')
{
num=num*10+s[i++]-'0';
}
res+=sign*num;
--i;
}
else if(c=='+')
{
sign=1;
}
else if(c=='-')
{
sign=-1;
}
else if(c=='(')
{
stk.push(res);
stk.push(sign);
res=0;
sign=1;
}
else if(c==')')
{
res*=stk.top();
stk.pop();
res+=stk.top();
stk.pop();
}
}
return res;
}
};

  参考:http://www.cnblogs.com/grandyang/p/4570699.html

224 Basic Calculator 基本计算器的更多相关文章

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

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

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

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

  3. 224. Basic Calculator + 227. Basic Calculator II

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

  4. [LeetCode] 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 解题报告(Python)

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

  7. Java for LeetCode 224 Basic Calculator

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

  8. (medium)LeetCode 224.Basic Calculator

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

  9. 224. Basic Calculator

    题目: Implement a basic calculator to evaluate a simple expression string. The expression string may c ...

随机推荐

  1. js 时钟特效

      时钟特效 CreateTime--2018年2月24日15:11:23 Author:Marydon 实现方式:都是基于HTML5的canvas标签实现的 款式一 借助jQuery插件实现 < ...

  2. 解决SVN Cleanup时遇到错误信息:Cleanup failed to process the following paths:xxxxxxx Previous operation has not finished: run 'cleanup' if it was interrupted Please execute the 'Cleanup' command.

    解决SVN Cleanup时遇到错误信息:Cleanup failed to process the following paths:xxxxxxx Previous operation has no ...

  3. win7-64bit安装comtypes的问题

    Update 28/12/2014: Please download the latest comtypes 1.1.1 from https://pypi.python.org/pypi/comty ...

  4. dm385的分辨率切换

    建议用两个RSZ的输出来完成切换分辨率功能,帧率可以通过软件丢帧来实现. 两个SWMS增加了两个1080p60的读和写,对系统影响是比较大的. http://www.deyisupport.com/q ...

  5. CodeSmith连Oracle

    据说CodeSmith连Oracle特别麻烦,什么WIN7下不行,64位下不行.之前有个同事为了用上CodeSmith,还特地装了个XP虚拟机. 其实,还是那个连接串的问题. 操作系统64位,就要用6 ...

  6. linux输入yum后提示: -bash: /usr/bin/yum: No such file or directory的解决方案

    linux输入yum后提示: -bash: /usr/bin/yum: No such file or directory的解决方案 今天在安装程序时,发现有一个插件未安装,我就随手敲了一个命令,看都 ...

  7. Lucene Core Solr

    Apache Lucene - Welcome to Apache Lucene https://lucene.apache.org/ The Apache LuceneTM project deve ...

  8. struts2的一些小问题

    1.action和ValueStack的关系2.ValueStack的类set()方法和setValue()方法的区别3.ValueStack的类push()方法的作用4.从ValueStack对象中 ...

  9. 【转】Google 发布 Android 性能优化典范(比较老,但很实用)

    2015年伊始,Google发布了关于Android性能优化典范的专题, 一共16个短视频,每个3-5分钟,帮助开发者创建更快更优秀的Android App.课程专题不仅仅介绍了Android系统中有 ...

  10. 使用C#开发HTTP服务器系列之访问主页

    各位朋友大家好,我是秦元培,欢迎大家关注我的博客,我的博客地址是http://qinyuanpei.com.在这个系列文章的第一篇中,我们着重认识和了解了HTTP协议,并在此基础上实现了一个可交互的W ...