LeetCode Basic Calculator
原题链接在这里:https://leetcode.com/problems/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
题解:
思路:1. 遇到数字位,看后一位是否为数字,若是位数字,当前位需要进十.
2. 开始设sign = 1,若遇到 ' - ', sign 改为 -1,若遇到 '+',sign改回1.
3. 遇到 '(', 压栈,先压之前的res,后压sign,然后初始化res和sign.
4. 遇到')' ,出栈,当前res先乘pop() 出来的 sign,再加上pop()出来的之前结果.
5. Encountering space, just continue.
Note:1.遇到数字时需要生成一个新的cur 用来存储当前数,随后再用cur求res,不能直接求res, '-' 时会出问题.
Time Complexity: O(s.length()).
Space: O(s.length()), stack space.
AC Java:
public class Solution {
public int calculate(String s) {
if(s == null || s.length() == 0){
return -1;
}
int res = 0;
int sign = 1;
Stack<Integer> stk = new Stack<Integer>();
for(int i = 0; i<s.length(); i++){
char c = s.charAt(i);
if(Character.isDigit(c)){
int cur = (int)(c-'0');
while(i+1 < s.length() && Character.isDigit(s.charAt(i+1))){
cur = cur*10 + (int)(s.charAt(i+1) - '0');
i = i+1;
}
res += sign*cur;
}else if(c == '-'){
sign = -1;
}else if(c == '+'){
sign = 1;
}else if(c == '('){
stk.push(res);
res = 0;
stk.push(sign);
sign = 1;
}else if(c == ')'){
res = res*stk.pop() + stk.pop();
}
}
return res;
}
}
类似Evaluate Reverse Polish Notation.
LeetCode Basic Calculator的更多相关文章
- [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 ...
- LeetCode Basic Calculator II
原题链接在这里:https://leetcode.com/problems/basic-calculator-ii/ Implement a basic calculator to evaluate ...
- [LeetCode] Basic Calculator III 基本计算器之三
Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...
- [LeetCode] Basic Calculator IV 基本计算器之四
Given an expression such as expression = "e + 8 - a + 5" and an evaluation map such as {&q ...
- [LeetCode] Basic Calculator & Basic Calculator II
Basic Calculator Implement a basic calculator to evaluate a simple expression string. The expression ...
- LeetCode——Basic Calculator II
Description: Implement a basic calculator to evaluate a simple expression string. The expression str ...
- LeetCode——Basic Calculator
Description: Implement a basic calculator to evaluate a simple expression string. The expression str ...
- LeetCode() Basic Calculator 不知道哪里错了
class Solution {public: int calculate(string s) { stack<int> num; stack<ch ...
随机推荐
- SSH自定义分页标签
本文参考文章:http://blog.csdn.net/qjyong/article/details/3240303 一.标签处理类: package cn.conris.sys.form; impo ...
- 第十六章 PHP 操作MySQL
学习要点:1.PHP 连接到MySQL2.增删改查3.其他常用函数 如果你已经具有了使用PHP.SQL 和MySQL 的丰富经验,现在就可以把所有这些技术组合在一起.PHP 与MySQL 之间稳固的集 ...
- 暑假训练round 3 题解
今天做题运气出奇的好,除了几处小错误调试之后忘记改掉了……最后还AK了……虽然题目不难,学长也说是福利局,但是对个人的鼓励作用还是挺大的……至此暑假训练就结束了,也算没有遗憾……. 题解如下: Pro ...
- 2016.04.27,英语,《Vocabulary Builder》Unit 19
bio, comes from the Greek word for 'life'. biosphere ['baɪoʊsfɪr] n. 生物圈: biology [baɪ'ɑːlədʒi] n. 生 ...
- UE4.7的IOS发布和调试的相关问题
UE4.7以后正式源码免费了,正好最近工作也在做这部分,ue4的官方文档虽然有一部分ios平台的资料,那也只是通过编辑器来发布或预览一类,但手游程序员都知道,一些cpu和gpu性能上的调试是在所难免的 ...
- Gmail 启用 POP 标准配置说明:
接收邮件 (POP3) 服务器 - 要求 SSL:pop.gmail.com使用 SSL:是端口:995 发送邮件 (SMTP) 服务器 - 要求 TLS 或 SSL:smtp.gmail.com使用 ...
- ip sevices
http://www.ip138.com/ip2city.asp http://www.bliao.com/ip.phtml http://www.whereismyip.com/ http://ww ...
- Solr 实现
Solr分页与高亮(使用SolrNet实现) 本节我们使用Asp.net MVC实现Solr客户端查询,建议使用SolrNet这个客户端,开源地址在:https://github.com/mausch ...
- love yy-kiss 医生出差二期
医生出差二期,做了一半撤出去做互联网医院了 http://confluence.haodf.net/pages/viewpage.action?pageId=15801359 医生出差二期 没设置数 ...
- 灰度图像 Grayscale Binary_image
https://en.wikipedia.org/wiki/Grayscale https://zh.wikipedia.org/wiki/灰度图像 In photography and comput ...