leetcode_Basic Calculator
题目:
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.
思路:栈的使用,括号的优先级最高。
class Solution {
public:
int calculate(string s) {
int len = s.length();
stack<int> st;
int i = 0;
int result = 0;
while(i<len)
{
int sum = 0;
if(s.at(i)>='0'&&s.at(i)<='9')
{
int j = i+1;
sum = s.at(i)-'0';
while(j<=len-1&&s.at(j)>='0'&&s.at(j)<='9')
{
sum = (sum*10 + (s.at(j) - '0'));
j++;
}
// cout<<sum<<endl;
//以上计算数字字符串转化为数字
if(!st.empty()&&(char)st.top()=='+')
{
st.pop();
result = st.top()+sum;
st.pop();
st.push(result);
}
else if(!st.empty()&&(char)st.top()=='-')
{
st.pop();
result = st.top()-sum;
st.pop();
st.push(result);
}
else
{
st.push(sum);
}
i = j;
} else if(s.at(i)==' ')
{
i++;
}
else if(s.at(i)=='+'||s.at(i)=='-')
{
st.push((int)s.at(i));
i++;
}
else if(s.at(i)=='(')
{
st.push((int)s.at(i));
i++;
}
else if(s.at(i)==')')
{
int temp = st.top();
if(!st.empty())
st.pop();
if(!st.empty())
st.pop();
if(!st.empty()&&st.top()=='+')
{
st.pop();//去掉
temp = temp+(st.top());
st.pop();
st.push(temp);
}
else if(!st.empty()&&st.top()=='-')
{
st.pop();//去掉
temp = (st.top())-temp;
st.pop();
st.push(temp);
}
else
{
st.push(temp);
}
i++;
}
}
return st.top();
}
};
leetcode_Basic Calculator的更多相关文章
- leetcode_Basic Calculator II
题目: Implement a basic calculator to evaluate a simple expression string. The expression string conta ...
- [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 ...
- Basic Calculator II
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
- Windows Universal 应用 – Tip Calculator
声明 以下内容取材于 Bob Tabor 的课程<Windows Phone 8.1 Development for Absolute Beginners>,链接地址为:http://ww ...
- Calculator(1.5)
Calculator(1.5) Github链接 ps.负数的处理未完成 解题过程中遇到的困难和解决 <stack>的使用: 认真研究了栈,基本上掌握了用法,与<queue>的 ...
- Calculator(1.0)
Calculator(1.0) Github链接 解题过程中遇到的困难 对于c++中类和对象的使用不够明确,看了c++的视频教程学会了用类和对象来写程序. 不会使用<queue>,在网上查 ...
- 数据结构与算法(1)支线任务2——Basic Calculator
题目:https://leetcode.com/problems/basic-calculator/ Implement a basic calculator to evaluate a simple ...
- calculator
#include <stdio.h> #include <stdlib.h> typedef enum { FALSE = , TRUE }BOOL; void calcula ...
随机推荐
- cocos2dx集成友盟社会化分享图片崩溃问题
本人不懂oc,一步一步按照友盟的文档做,好不容易把分享做好了,结果在 分享图片的时候宕掉了.各种测试,搞了一下午终于搞定了. 如下是友盟文档的做法,错误就在[UIImage imageNamed:@& ...
- [Linux内核]软中断与硬中断
转自:http://blog.csdn.net/zhangskd/article/details/21992933 本文主要内容:硬中断 / 软中断的原理和实现 内核版本:2.6.37 Author: ...
- jQuery 中 attr() 和 prop() 方法的区别<转>
前几天,有人给 Multiple Select 插件 提了问题: setSelects doesn't work in Firefox when using jquery 1.9.0 一直都在用 jQ ...
- PHP——注册页面,审核页面,登录页面:加Session和Cookie
实现效果: 用户注册信息,管理员核对信息审核通过后,可实现注册的用户名和密码的成功登陆,利用session和cookie获取用户信息并且不能跳过登录页面直接进入主页面 1.Session存储在服务器可 ...
- hdu6149 Valley Numer II 分组背包+状态压缩
/** 题目:hdu6149 Valley Numer II 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6149 题意: 众所周知,度度熊非常喜欢图. ...
- css样式整理
字体属性:(font) 大小 {font-size: x-large;}(特大) xx-small;(极小) 一般中文用不到,只要用数值就可以,单位:PX.PD 样式 {font-style: obl ...
- JVM与外界通过数据通道进行数据交换
使用I/O流访问file中的内容. JVM与外界通过数据通道进行数据交换. 分类: 按流分为输入流和输出流: 按传输单位分为字节流和字符流: 还可以分为节点流和过滤流. 节点流:负责数据源和程序之间建 ...
- 【BZOJ】1492: [NOI2007]货币兑换Cash(cdq分治)
http://www.lydsy.com/JudgeOnline/problem.php?id=1492 蒟蒻来学学cdq神算法啊.. 详见论文 陈丹琦<从<Cash>谈一类分治算法 ...
- 学习:record用法
详情请参考官网:http://www.erlang.org/doc/reference_manual/records.html http://www.erlang.org/doc/programmin ...
- JavaScript的parseint()函数
定义和用法 parseInt() 函数可解析一个字符串,并返回一个整数. 语法 parseInt(string, radix) 参数 描述 string 必选项.要转换为数字的字符串. radix 可 ...