【LeetCode】Basic Calculator && Basic Calculator II
2015-06-24 11:01
471 查看
1、Basic Calculator
Total Accepted: 3726 Total Submissions: 24053 My Submissions Question Solution
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:
Note: Do not use the eval built-in library function.
2、Basic Calculator II
Total Accepted: 1046 Total Submissions: 5287 My Submissions Question Solution
Implement a basic calculator to evaluate a simple expression string.
The expression string contains only non-negative integers, +, -, *, / operators and empty spaces . The integer division should truncate toward zero.
You may assume that the given expression is always valid.
Some examples:
Note: Do not use the eval built-in library function.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
【解题思路】
1带括号,不带乘除。2不带括号,带乘除
下一个升级版有可能是带括号又带乘除。
1、每次计算都需要判断当前符号是不是括号,如果是括号就先算括号内的值。在discuss中看到一个很漂亮的代码。参考Iterative Java solution with stack
2、遇乘除计算。然后按照顺序计算一次加减的值。
1、Java AC
Total Accepted: 3726 Total Submissions: 24053 My Submissions Question Solution
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.
2、Basic Calculator II
Total Accepted: 1046 Total Submissions: 5287 My Submissions Question Solution
Implement a basic calculator to evaluate a simple expression string.
The expression string contains only non-negative integers, +, -, *, / operators and empty spaces . The integer division should truncate toward zero.
You may assume that the given expression is always valid.
Some examples:
"3+2*2" = 7 " 3/2 " = 1 " 3+5 / 2 " = 5
Note: Do not use the eval built-in library function.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
【解题思路】
1带括号,不带乘除。2不带括号,带乘除
下一个升级版有可能是带括号又带乘除。
1、每次计算都需要判断当前符号是不是括号,如果是括号就先算括号内的值。在discuss中看到一个很漂亮的代码。参考Iterative Java solution with stack
2、遇乘除计算。然后按照顺序计算一次加减的值。
1、Java AC
public class Solution { public int calculate(String s) { Stack<Integer> stack = new Stack<Integer>(); int len = s.length(); int result = 0; int sign = 1; int num = 0; for (int i = 0; i < len; i++) { char c = s.charAt(i); if (c >= '0' && c <= '9') { num = 10 * num + c - '0'; } else if (c == '+') { result += sign * num; sign = 1; num = 0; } else if (c == '-') { result += sign * num; sign = -1; num = 0; } else if (c == '(') { stack.push(result); stack.push(sign); result = 0; sign = 1; } else if (c == ')') { result += sign * num; num = 0; result *= stack.pop(); result += stack.pop(); } } if (num != 0) { result += sign * num; } return result; } }2、Java AC
public class Solution { public int calculate(String s) { if (s == null || "".equals(s.trim())) { return 0; } s = s.replaceAll("\\s", ""); List<Long> list1 = new ArrayList<Long>(); List<Character> list2 = new ArrayList<Character>(); int len = s.length(); int i = 0; while (i < len) { char c = s.charAt(i); if (c >= '0' && c <= '9') { long array[] = getNum(s, i); list1.add(array[0]); i = (int) array[1]; } else if (c == '+' || c == '-') { list2.add(c); } else if (c == '*' || c == '/') { int size = list1.size(); long num1 = list1.get(size - 1); long array[] = getNum(s, i + 1); long num2 = array[0]; i = (int) array[1]; long num = c == '*' ? num1 * num2 : num1 / num2; list1.set(size - 1, num); } i++; } int size1 = list1.size(); int size2 = list2.size(); long preNum = list1.get(0); for (i = 0; i < size2; i++) { if (list2.get(i) == '+') { preNum += list1.get(i + 1); } else { preNum -= list1.get(i + 1); } } return (int) preNum; } private long[] getNum(String s, int curPos) { StringBuffer sb = new StringBuffer(); int len = s.length(); while (curPos < len && s.charAt(curPos) >= '0' && s.charAt(curPos) <= '9') { sb.append(s.charAt(curPos)); curPos++; } String newStr = sb.toString(); if (newStr.length() > 0) { return new long[]{Long.parseLong(newStr), curPos - 1}; } return new long[]{0, curPos - 1}; } }
相关文章推荐
- 四种强制类型转换的总结
- 进程的组成部分:
- 【Effective C++读书笔记】序
- Maven pom.xml 配置详解
- C语言-指针,
- PHP将Excel导入数据库及数据库数据导出至Excel的方法
- UITableViewCell点击时背景颜色
- 产品设计中的头脑风暴这样做比较高效
- nginx启动,重启,关闭命令 (另:kill -USR2 的作用)
- Java设计模式透析之 —— 单例(Singleton)
- 链路层链路发现协议(LLDP)讲解
- Leetcode--easy系列3
- plughw:0,0
- [安卓]手机管家(十五)软件管理 NO 2
- C# 中重载自增自减操作符的具体运算原理 ----从C++程序员的角度看C#自增操作符重载的实质
- XML万能数据库设计
- Linux服务器性能评估
- linux并发连接数:Linux下高并发socket最大连接数所受的各种限制
- 词:柳梢青(赠2015届毕业的学长学姐们)
- Docker Networking Rules: EXPOSE, -p, -P, –link