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 ...
随机推荐
- request的生存期只限于服务器跳转
症状: 刚才想做一个实验,在a.jsp中向request添加属性(页面编码为UTF-8),在b.jsp中删除该属性(页面编码为ISO-8859-1),通过ServletRequestAttribute ...
- jquery 查找元素
/************ 查找父元素 *************/ //closest()方法 $("#mytd1").bind("click",functi ...
- poj2082单调栈
本来实在做后缀数组的题目的,不巧,碰到了pku3415这题,需要用到单调栈来维护,但是之前又没有学习过单调栈这方面的知识,于是水了几题....... 题意:给你一些连续的小矩形,高宽不定,求最大的矩形 ...
- 个别图片IE中无法显示问题
今天有人保障,某些图片在IE下无法打开,但是其他浏览器均没有问题.以前还真没遇到过这类问题,从上至下查看了一遍,能排除的因素基本都排除了,还是不知道为什么不能显示,真是奇怪了.最后注意到无法显示的图片 ...
- 梯度下降算法到logistic回归
http://sbp810050504.blog.51cto.com/2799422/1608064/ http://blog.csdn.net/dongtingzhizi/article/detai ...
- This表示当前对象
This表示当前对象. Public void printNum(){ Int number=40: System.out.println(this.number); } 此时打印的是实例变量,而非局 ...
- 部署到服务器-执行脚本-脚本传递参数-需要base on 执行传入的参数(被测环境的ip)
测试脚本 # !/usr/bin/python # -*- coding:utf-8 -*- import sys sys.path.append("..") from utils ...
- Html制作简单而漂亮的登录页面
先来看看样子. html源码: <!DOCTYPE html> <html lang="en"> <head> <meta charset ...
- springframework resource
文件资源操作 Spring 定义了一个 org.springframework.core.io.Resource 接口,Resource 接口是为了统一各种类型不同的资源而定义的,Spring ...
- AuthorizeAttribute示例
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...