Different Ways to Add Parentheses——Leetcode
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".
((2-1)-1) = 0
(2-(1-1)) = 2
Output: [0, 2]
Example 2
Input: "2*3-4*5"
(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
Output: [-34, -14, -10, -10, 10]
题目大意:给定一个表达式,让你随便在上面加括号,然后算所有可能的结果。
解题思路:
这题如果顺着题目的意思,自己去加括号然后算表达式的值那就完了,掉坑里出不来了,其实就是分治,举个例子:
对表达式,找到一个运算符,然后计算式子两边的所有可能的值,然后把两边分别乘起来就可以了,递归的计算两边的值。
题目的要求就是计算所有的。
public static List<Integer> diffWaysToCompute(String input) {
List<Integer> res = new ArrayList<>();
if(input==null||input.length()==0){
return res;
}
for(int i=0;i<input.length();i++){
char c = input.charAt(i);
if(!Character.isDigit(c)){
List<Integer> pre = diffWaysToCompute(input.substring(0,i));
List<Integer> post = diffWaysToCompute(input.substring(i+1,input.length()));
for(int f : pre){
for(int l : post){
if(c=='+'){
res.add(f+l);
}else if(c=='-'){
res.add(f-l);
}else if(c=='*'){
res.add(f*l);
}
}
}
}
}
if(res.size()==0){
res.add(Integer.valueOf(input));
}
Collections.sort(res);
return res;
}
Different Ways to Add Parentheses——Leetcode的更多相关文章
- 【LeetCode】241. Different Ways to Add Parentheses
Different Ways to Add Parentheses Given a string of numbers and operators, return all possible resul ...
- 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 ...
- 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]* ...
- 241. Different Ways to Add Parentheses
241. Different Ways to Add Parentheses https://leetcode.com/problems/different-ways-to-add-parenthes ...
- LC 241. Different Ways to Add Parentheses
Given a string of numbers and operators, return all possible results from computing all the differen ...
- [LeetCode] 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 添加括号的不同方式
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 (Divide and Conquer)
https://leetcode.com/problems/different-ways-to-add-parentheses/ Given a string of numbers and opera ...
随机推荐
- SQL查询一些浅薄的结论
一些简单的测试结论 在本机经过一些简单的测试,记录数6W条,得出以下结论,不同的硬件环境和数据记录数,可能会有不一样的结论 1.in, or, exists, like, not in , not e ...
- 【转自CSDN】深入 Microsoft.VisualBasic.Strings.StrConv 簡繁轉換
深入 Microsoft.VisualBasic.Strings.StrConv 簡繁轉換 昨天又遇到一個簡繁轉換的需求, 雖然這個問題以前已經處理過了, 但是以前是用自己建立的 b52gb 和 gb ...
- Oracle 的merge into 用法
1.merge into的用途 Merge是一个非常有用的功能,与DB2中的merge into功能几乎一样,与Mysql里的insert into on duplicate key也很类似.MERG ...
- 寒假的ACM训练(一)
今天开始ACM训练,选择了刘汝佳的<挑战编程>,暂时算是开始了. 测评的网址: http://www.programming-challenges.com 第一个题目是水题啦.3n+1. ...
- Trac的使用思考
为什么要使用Trac? 这是由于在软件开发当中需要设定阶段性的目标,和进行文档的统一归档,并且便于进行多人协同工作. Trac是基于Python开发的.所以要使用Trac就需要对于Python有一定的 ...
- Gtest打桩函数
假设Client的定义如下 class Client { ...... public: virtual bool GetData(std::string& data); ...... }; 我 ...
- LICAppInfo
[Common] Enabled=true All=true [AppList] #AppName #Enabled #Validit ...
- mysql进阶1
在我们用php处理数据的时候总会遇到些比较麻烦的事情,比如:两个二维数组,一个装的是文章分类表内容,一个装的是文章列表,有关联字段,完全等值,要求在列表文章的时候同时能在标题的前面显示栏目名称,此时循 ...
- shell编程的一些例子1
1.$0-$9及$# $@的使用 demo_arg 内容 #!/bin/bash echo "程序名:$0" echo "命令传递参数个数:$#" echo & ...
- 在Docker下部署Nginx
在Docker下部署Nginx 在Docker下部署Nginx,包括: 部署一个最简单的Nginx,可以通过80端口访问默认的网站 设置记录访问和错误日志的路径 设置静态网站的路径 通过proxy_p ...