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 ...
随机推荐
- oracle 快速批量插入复杂数据的内容
最近迷上一种批量插入的方法,一句sql解决,将需要插入的数据用with as 的方式查出来,不管多么复杂的sql,都可以用临时表的方式查出来,然后直接插入,这样代码更加清晰 流程也简单 insert ...
- FreeRTOSConfig 配置文件详解
以下转载自安富莱电子: http://forum.armfly.com/forum.php 本章节为大家讲解 FreeRTOS 的配置文件 FreeRTOSConfig.h 中每个选项的作用.初学的话 ...
- Android学习之两款下拉刷新库分享
昨天没有写博客.心里非常罪过呀,今天给大家写两种比較常见的下拉刷新的用法.一款是SwipeRefreshLayout,一款是CircleRefreshLayout. SwipeRefreshLayou ...
- EasyUI Tree checkbox node
tree插件允许你创建checkbox tree,如果你点击节点的checkbox,被点击的节点信息得到下和上的继承.例如,点击tomato节点的checkbox,你可以看到vegetables节点现 ...
- C++ 类的继承二(赋值兼容性原则)
//赋值兼容性原则 #include<iostream> using namespace std; class PointA{ public: PointA(){ x = ; y = ; ...
- android sdk屏幕截图工具
调用android sdk中的工具,在开发板上截图. 使用usb线连接android设备,打开adb调试. 进入目录 sdk/tools/ 运行 traceview.bat 运行 uiautomato ...
- ORCLE 表中列的修改(非常全面哦)
今天下午主要做了个实验,是针对 测试表的列,进行添加,修改,删除的.做法如下: 增加一列: alter table emp4 add test varchar2(10); 修改一列: alter ta ...
- jQuery实现高亮显示网页关键词的方法
本文实例讲述了jQuery实现高亮显示网页关键词的方法.分享给大家供大家参考.具体如下: 这是一款基于jquery实现的高亮显示网页上搜索关键词的代码,当你在文本框中输入的时候,如果下面的正文中包括你 ...
- 【代码备份】pocs.m
超分辨率算法代码 POCS算法,凸集投影法. pocs.m,没有调用的代码,没看懂..只有这个函数..抱歉. function y = pocs(s,delta_est,factor) % POCS ...
- 使用CAtlRegExp类进行密码校验
前言 最近做了一个小需求,新建用户时输入的密码必须包含数字.小写字母.大写字符以及特殊字符,目的是为了增强密码的强度,如果没有其中一项,就需要弹出窗口进行提示. 正则表达式 对于此类 ...