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的更多相关文章

  1. 【LeetCode】241. Different Ways to Add Parentheses

    Different Ways to Add Parentheses Given a string of numbers and operators, return all possible resul ...

  2. 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 ...

  3. 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]* ...

  4. 241. Different Ways to Add Parentheses

    241. Different Ways to Add Parentheses https://leetcode.com/problems/different-ways-to-add-parenthes ...

  5. LC 241. Different Ways to Add Parentheses

    Given a string of numbers and operators, return all possible results from computing all the differen ...

  6. [LeetCode] Different Ways to Add Parentheses 添加括号的不同方式

    Given a string of numbers and operators, return all possible results from computing all the differen ...

  7. [LeetCode] 241. Different Ways to Add Parentheses 添加括号的不同方式

    Given a string of numbers and operators, return all possible results from computing all the differen ...

  8. (medium)LeetCode 241.Different Ways to Add Parentheses

    Given a string of numbers and operators, return all possible results from computing all the differen ...

  9. 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 ...

随机推荐

  1. 学习java随笔第三篇:java的基本数据类型

    数据类型 一:整型 1.十进制 2.八进制 八进制数是满8进1,包含0~7的8个数字,在整数前面添加一个"0",表示是八进制数. 3.十六进制 十六进制数是满16进1,包含0~9, ...

  2. .NET平台下几种SOCKET模型的简要性能供参考

    转载自:http://www.cnblogs.com/asilas/archive/2006/01/05/311309.html .NET平台下几种SOCKET模型的简要性能供参考 这个内容在cnbl ...

  3. tomcat的server.xml详解

    Tomcat服务器是由一系列可配置的组件构成,其核心组件是Catalina   Servlet容器,它是所有其他Tomcat组件的顶层容器.Tomcat的组件可以在<CATALINA_HOME& ...

  4. TCP/IP状态转换图

  5. [C#学习]在多线程中如何调用Winform[转]

    问题的产生: 我的WinForm程序中有一个用于更新主窗口的工作线程(worker thread),但文档中却提示我不能在多线程中调用这个form(为什么?),而事实上我在调用时程序常常会崩掉.请问如 ...

  6. sharepoint给文档库每个数据条添加权限

    前言 老大任务,做一个读取文档库把里面的每一条数据添加权限.挺起来很简单,但是做起来,还是很简单,哈哈.因为我没有接触过这些代码,所以得不断的请教了.大题明白了,简单实现了一下,应用控制台先做了一下简 ...

  7. Android Handler、Lopper消息驱动机制

    Android应用程序是通过消息来驱动的,系统为每一个应用程序维护一个消息队例(MesageQueue),应用程序的主线程不断地从这个消息队例中获取消息(Mesage),然后对这些消息进行处理(Han ...

  8. python中os模块的常用接口和异常中Exception的运用

    1.os.path.join(arg1, arg2) 将arg1和arg2对应的字符串连接起来并返回连接后的字符串,如果arg1.arg2为变量,就先将arg1.arg2转换为字符串后再进行连接. 2 ...

  9. learn-python3

    # learn-python3   这是我初学Python时写的一套Python基础示例程序.主要基于廖雪峰老师的Python3教程和<<深入理解Python>>. 感谢! 下 ...

  10. C#让程序自动在管理员权限下运行

    windows 7和vista提高的系统的安全性,同时需要明确指定“以管理员身份运行”才可赋予被运行软件比较高级的权限,比如访问注册表等.否则,当以普通身份运行的程序需要访问较高级的系统资源时,将会抛 ...