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 ...
随机推荐
- Java中实现文件上传下载的三种解决方案
第一点:Java代码实现文件上传 FormFile file=manform.getFile(); String newfileName = null; String newpathname=null ...
- openstack-L版安装
参照官方install document: http://docs.openstack.org/liberty/install-guide-rdo/ 实验环境:centos7.2 桥接: 192.16 ...
- java实现吸血鬼数字
public class Vempire { public static void main(String[] arg) { String[] ar_str1, ar_str2; ; int from ...
- Html - 仿QQ空间右下角工具浮动块
仿QQ空间右下角工具浮动块 <style type="text/css"> .cy-tp-area>.cy-tp-fixbtn>.cy-tp-text { ...
- SVN客户端常用命令
1. 将文件checkout到本地目录 svn checkout path(path是服务器上的目录) 例如: cd /home/www #进入准备获取的项目路径 svn checkout svn: ...
- 采用HSV生成随机颜色
使用hsv/hsb生成随机颜色,并排除靠近黑白两色的色值 public static String randomColor(){ int max = 25500000 ; Random rand = ...
- nor flash和nand flash的区别
NOR和NAND是现在市场上两种主要的非易失闪存技术.Intel于1988年首先开发出NOR flash技术,彻底改变了原先由EPROM和EEPROM一统天下的局面.紧接着,1989年,东芝公司发表了 ...
- 利用EXCEL表实现网页数据采集到MYSQL数据库
先复制页面表格数据到EXCEL中,比如 2012-1-4 52.7 52.7 49 48.83 190007 9506968 2012-1-5 48.86 49.79 45.72 45.6 62325 ...
- memcached学习笔记3--telnet操作memcached
方式: 一.telnet访问memcached缓存系统(主要用于教学,不讨论) telnet 127.0.0.1 11211 => telnet IP地址 端口号 //往Memcache ...
- Solr 连接数据库
实际工程应用中,从数据库导出数据创建索引再常见不过了,现在实验一下从数据库导入数据创建索引. 一.版本说明 Solr版本:4.7.0 数据库:sqlserver2005 二.配置步骤 1. 准备的j ...