这种题都要设置一个符号位的变量

224. Basic Calculator

设置数值和符号两个变量,遇到左括号将数值和符号加进栈中

class Solution {
public:
int calculate(string s) {
stack<int> st;
int res = ,flag = ;
for(int i = ;i < s.size();i++){
if(s[i] >= '' && s[i] <= ''){
int num = ;
while(i < s.size() && s[i] >= '' && s[i] <= ''){
num = num* + flag * (s[i] - '');
i++;
}
res += num;
i--;
}
else if(s[i] == '+')
flag = ;
else if(s[i] == '-')
flag = -;
else if(s[i] == '('){
st.push(res);
st.push(flag);
res = ,flag = ;
}
else if(s[i] == ')'){
res *= st.top();
st.pop();
res += st.top();
st.pop();
}
}
return res;
}
};

227. Basic Calculator II

乘除法有优先级,这个时候需要将这些数值弹出

与上一题不同,这个题是把所有的结果存入进stack中

class Solution {
public:
int calculate(string s) {
long res = ,num = ;
char op = '+';
stack<int> st;
for(int i = ;i < s.size();i++){
if(s[i] >= '' && s[i] <= '')
num = num * + s[i] - '';
if(s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/' || i == s.size() - ){
if(op == '+')
st.push(num);
if(op == '-')
st.push(-num);
if(op == '*' || op == '/'){
int tmp = op == '*' ? st.top() * num : st.top() / num;
st.pop();
st.push(tmp);
}
op = s[i];
num = ;
}
}
while(!st.empty()){
res += st.top();
st.pop();
}
return res;
}
};

leetcode 224. Basic Calculator 、227. Basic Calculator II的更多相关文章

  1. leetcode 169. Majority Element 、229. Majority Element II

    169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...

  2. leetcode 79. Word Search 、212. Word Search II

    https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...

  3. leetcode 54. Spiral Matrix 、59. Spiral Matrix II

    54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...

  4. leetcode 263. Ugly Number 、264. Ugly Number II 、313. Super Ugly Number 、204. Count Primes

    263. Ugly Number 注意:1.小于等于0都不属于丑数 2.while循环的判断不是num >= 0, 而是能被2 .3.5整除,即能被整除才去除这些数 class Solution ...

  5. leetcode 344. Reverse String 、541. Reverse String II 、796. Rotate String

    344. Reverse String 最基础的旋转字符串 class Solution { public: void reverseString(vector<char>& s) ...

  6. leetcode 131. Palindrome Partitioning 、132. Palindrome Partitioning II

    131. Palindrome Partitioning substr使用的是坐标值,不使用.begin()..end()这种迭代器 使用dfs,类似于subsets的题,每次判断要不要加入这个数 s ...

  7. leetcode 280.Wiggle Sort 、324. Wiggle Sort II

    Wiggle Sort: 注意:解法一是每次i增加2,题目不是保证3个3个的情况,而是整个数组都要满足要求. 解法一错误版本: 如果nums的长度是4,这种情况下nums[i+1]会越界.但是如果你用 ...

  8. leetcode 1.Two Sum 、167. Two Sum II - Input array is sorted 、15. 3Sum 、16. 3Sum Closest 、 18. 4Sum 、653. Two Sum IV - Input is a BST

    1.two sum 用hash来存储数值和对应的位置索引,通过target-当前值来获得需要的值,然后再hash中寻找 错误代码1: Input:[3,2,4]6Output:[0,0]Expecte ...

  9. leetcode 62. Unique Paths 、63. Unique Paths II

    62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...

随机推荐

  1. 浅析JavaScript工厂模式

    这里主要介绍两种工厂模式,第一种“简单工厂模式”,第二种“工厂方法模式” 简单工厂模式 1.定义 由一个工厂对象决定对象创建某一种产品对象的的实例.主要用来创建同一类对象. 2.具体需求 现在有一个登 ...

  2. 分享基于MemoryCache(内存缓存)的缓存工具类,C# B/S 、C/S项目均可以使用!

    using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Caching; usi ...

  3. Python3+Selenium2完整的自动化测试实现之旅(六):Python单元测试模块Unittest运用

    一.Unittest单元测试框架简介 Unitest是Python下的一个单元测试模块,是Python标准库模块之一,安装完Python后就可以直接import该模块,能在单元测试下编写具体的测试用例 ...

  4. HTTP与HTTPS的理解

    最近一直也在面试的过程中,可能由于各个方面的问题,导致没有时间抽出更新博客,今天开始陆续更新!!!以后自己的博客,会向React Native,swift ,以及H5延展,成为一个全栈的技术人员.本篇 ...

  5. IEnumerable<T>和IQueryable<T>区分

    LINQ查询方法一共提供了两种扩展方法,在System.Linq命名空间下,有两个静态类:Enumerable类,它针对继承了IEnumerable<T>接口的集合进行扩展:Queryab ...

  6. year 和 weak year 的区别

    java 中使用 SimpleDateFormat 时,会遇到 year 和 week year 这两个概念,特此记录. google 答案: A week year is in sync with ...

  7. Maven(九)Eclipse创建Web项目(简单方式)

    1. 创建Maven项目(以简单方式) 2. 勾选WAR 3. 选择properties->projectFacts 此处的错误可忽略,配置好会会消失,主要缺失web.xml文件 4. 将框中选 ...

  8. 原生js获取元素非行内样式属性的方法

    获取当前对象的样式DOM标准中的全局方法 getComputedStyle(obj).width (获取元素的宽度),但在非标准IE浏览器(IE8)以下有兼容问题IE8以下要这样写 obj.curre ...

  9. python 练习 simple_server 判断路径及返回函数

    函数 routers 返回一个 urlpatterns 元组,里面包含了路径名和函数名:在 函数 application 中遍历 urlpatterns 元组,路径存在则返回函数名,不存在则返回 40 ...

  10. AEAI HR薪资汇总功能介绍

    1 概述 人力资源系统是一个公司重要的管理工具,而薪资管理是人力资源管理系统中最为核心的功能模块,它包括不同员工的薪资标准.薪资的组成部分,例如:对奖惩管理.保险和年假等员工必备的福利待遇进行统一管理 ...