LC 241. Different Ways to Add Parentheses
Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The valid operators are +
, -
and *
.
Example 1:
Input:"2-1-1"
Output:[0, 2]
Explanation:
((2-1)-1) = 0
(2-(1-1)) = 2
Example 2:
Input:"2*3-4*5"
Output:[-34, -14, -10, -10, 10]
Explanation:
(2*(3-(4*5))) = -34
((2*3)-(4*5)) = -14
((2*(3-4))*5) = -10
(2*((3-4)*5)) = -10
(((2*3)-4)*5) = 10
Runtime: 4 ms, faster than 77.90% of Java online submissions for Different Ways to Add Parentheses.
class Solution {
public List<Integer> diffWaysToCompute(String input) {
List<Integer> ret = new ArrayList<>();
for(int i=0; i<input.length(); i++){
if(input.charAt(i) == '+' ||
input.charAt(i) == '-' ||
input.charAt(i) == '*') {
String part1 = input.substring(0,i);
String part2 = input.substring(i+1);
List<Integer> part1ret = diffWaysToCompute(part1);
List<Integer> part2ret = diffWaysToCompute(part2);
for(int x : part1ret){
for(int y : part2ret){
switch (input.charAt(i)) {
case '+' : ret.add(x + y);
break;
case '-' : ret.add(x - y);
break;
case '*' : ret.add(x * y);
break;
}
}
}
}
}
if(ret.size() == 0) ret.add(Integer.valueOf(input));
return ret;
} }
LC 241. Different Ways to Add Parentheses的更多相关文章
- LN : leetcode 241 Different Ways to Add Parentheses
lc 241 Different Ways to Add Parentheses 241 Different Ways to Add Parentheses Given a string of num ...
- 241. Different Ways to Add Parentheses
241. Different Ways to Add Parentheses https://leetcode.com/problems/different-ways-to-add-parenthes ...
- leetcode 96. Unique Binary Search Trees 、95. Unique Binary Search Trees II 、241. Different Ways to Add Parentheses
96. Unique Binary Search Trees https://www.cnblogs.com/grandyang/p/4299608.html 3由dp[1]*dp[1].dp[0]* ...
- 【LeetCode】241. Different Ways to Add Parentheses
Different Ways to Add Parentheses Given a string of numbers and operators, return all possible resul ...
- [LeetCode] 241. Different Ways to Add Parentheses 添加括号的不同方式
Given a string of numbers and operators, return all possible results from computing all the differen ...
- 241. Different Ways to Add Parentheses——本质:DFS
Given a string of numbers and operators, return all possible results from computing all the differen ...
- (medium)LeetCode 241.Different Ways to Add Parentheses
Given a string of numbers and operators, return all possible results from computing all the differen ...
- [LeetCode#241]Different Ways to Add Parentheses
Problem: Given a string of numbers and operators, return all possible results from computing all the ...
- leetcode@ [241] Different Ways to Add Parentheses (Divide and Conquer)
https://leetcode.com/problems/different-ways-to-add-parentheses/ Given a string of numbers and opera ...
随机推荐
- JPA中的复杂查询
JPQL全称Java Persistence Query Language 基于首次在EJB2.0中引入的EJB查询语言(EJB QL),Java持久化查询语言(JPQL)是一种可移植的查询语言,旨在 ...
- 【url ---lib___】笔趣阁(抓取斗罗大陆完整)和(三寸天堂)
# coding=gbk #因为在黑屏下执行,所以代码会使用GBK url='http://www.biquge.info/10_10218/' UA={"User-Agent": ...
- MySQL备份--xtrabackup与mysqldump工具使用
MySQL备份----xtrabackup与mysqldump工具的使用 一.Xtrabackup8.0: 一个用于MySQL数据库物理热备的备份工具,支持MySQL.Percona server和M ...
- 三次样条插值 cubic spline interpolation
什么是三次样条插值 插值(interpolation)是在已知部分数据节点(knots)的情况下,求解经过这些已知点的曲线, 然后根据得到的曲线进行未知位置点函数值预测的方法(未知点在上述已知点自变量 ...
- V.24 V.35 ISDN E1 POS这些常见的广域网接口
转:http://blog.sina.com.cn/s/blog_bc1c78600101l2ss.html 广域网(Wide Area Network)是一种跨地区的数据通讯网络,通常是一个局域网到 ...
- Codeforces 1148 E - Earth Wind and Fire
E - Earth Wind and Fire 思路: 栈模拟 代码: #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC opti ...
- 关于C++ Builder Codegurad 问题的排查。
关于C++ BUILDER6 我目前不知道有什么特别好的内存排查工具.尤其为了对付memory leak, (Eurekalog 这个工具内存泄漏主要针对delphi,BCB配置比较繁琐). 除了BC ...
- GDB调试器教程
启动和退出GDBGDB(GNU Project Debugger)几乎适用于所有类Unix系统,小巧方便且不失功能强大,Linux/Unix程序员经常用它来调试程序. 总的来说有几下几种方法启动GDB ...
- ftp上传文件和下载文件
public class FtpService { #region Fields and attributes private readonly int BufLen = 2048; /// < ...
- [HBase]region compaction流程