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 ...
随机推荐
- Spring Cloud(一)服务的注册与发现(Eureka)
Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中涉及的配置管理.服务发现.断路器.智能路由.微代理.控制总线.全局锁.决策竞选.分布式会话和集 ...
- shiro系列二、身份验证和授权
一.身份验证 先来看看身份验证的流程 流程如下: 1.首先调用Subject.login(token)进行登录,其会自动委托给Security Manager,调用之前必须通过SecurityUtil ...
- Ubuntu系统---FeiQ安装记录
Ubuntu系统---FeiQ安装记录 linux下安装飞秋/飞鸽传书之类的软件iptux信使,可以与windows在一个局域网下聊天与传书文件,安装很简单. 首先,直接运行下面的语句即可:sudo ...
- eclipse安装Bug检查工具
第一步:下载spotbugs工具 打开eclipse>Help>Eclipse Matketplace 打开 Eclipse Marketplace 搜索 spotbugs,点击Insta ...
- vue中父组件使用props或者$attras向子组件中传值
知识点:vue中使用props或者$attras向子组件中传值 (1) props传值 子组件必须注册好要传的数据() props:['id'] (2)$attrs传值 该数据在props中,没有注册 ...
- 《深入理解Java虚拟机》之(二、垃圾收集器与内存分配策略)
程序计数器.虚拟机栈.本地方法栈3个区域随线程而生,随线程而灭,这几个区域的内存分配和回收都具备确定性,不需要过多考虑回收的问题,因为方法结束或者线程结束时,内存自然就跟着回收了,而java堆和方法区 ...
- Map遍历效率 : entrySet > keySet
1 //entrySet() 2 for (Entry<String, String> entry : map.entrySet()) { 3 Stri ...
- C# 截取屏幕方法
this.Visible = false; System.Threading.Thread.Sleep(); Bitmap bit = new Bitmap(Screen.PrimaryScreen. ...
- centos7部署mysql5.7一主多从
/usr/share/mysql/charsets/ 服务器 ip 操作系统 mysql Mysql_master 192.168.188.11 centos7 5.7 Mysql_slave1 19 ...
- Codeforces Round #590 (Div. 3) C. Pipes
链接: https://codeforces.com/contest/1234/problem/C 题意: You are given a system of pipes. It consists o ...