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 ...
随机推荐
- c函数声明前加typedef是什么情况
刚才看到APUE(高级UNIX环境编程)里面的apue.h中有一行 typedef void Sigfunc(int); 没搞懂什么意思 其实就是定义一个函数指针类型,等价于 typedef void ...
- C语言 面试
P1(多选)有如下定义 int a; int *b; 则下列哪些语句是正确的:A: b=&a;B: b=*a;C: b=(int*)a;D: *b=a; 思路如下:b是一个int类型指针,a是 ...
- httpclient4.5 的一些细节
本文转自:http://mercymessi.iteye.com/blog/2250161 httpclient是Apache下的一个用于执行http网络访问的一个工具包. 大致流程:新建一个http ...
- w3c html dom
http://www.w3school.com.cn/ SQL语句学习 http://www.w3school.com.cn/sql/sql_like.asp
- 一款基于jQuery的图片左右滑动焦点图
今天给大家分享一款基于jQuery的焦点图插件,这款jQuery焦点图插件的特点是可以多张图片左右滑动切换,可以点击切换按钮进行图片滑动,同时也支持图片自动切换.另外,这款jQuery焦点图是宽屏的, ...
- IoC是一个很大的概念,可以用不同的方式实现。其主要形式有两种:
IoC是一个很大的概念,可以用不同的方式实现.其主要形式有两种: ◇依赖查找:容器提供回调接口和上下文条件给组件.EJB和ApacheAvalon都使用这种方式.这样一来,组件就必须使用容器提供的AP ...
- html-include
import header <head> <link rel="import" href="header.html"> </hea ...
- hdu 1058:Humble Numbers(动态规划 DP)
Humble Numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- 利用flume+kafka+storm+mysql构建大数据实时系统
架构图
- maven 完整的settings.xml
maven 完整的settings.xml <?xml version="1.0" encoding="UTF-8"?> <!-- Licen ...