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. Mindjet MindManager 2012 从模板创建出现“Runtime Error pure virtual function call” 解决方法

    我的Mindjet MindManager 2012 Pro也就是MindManager10 在应用模板之后总会显示 Microsoft Visual C++ Runtime Library Runt ...

  2. apache的ab进行页面的压力测试

    参考http://www.cnblogs.com/yjf512/archive/2011/05/24/2055723.html apache/bin/ab ./ab –n 1000 –c 100 ht ...

  3. frameset标签代码实现网站跳转

    js代码1: document.writeln("<frameset rows=\"0, *\">"); document.writeln(&quo ...

  4. 精通 Oracle+Python,第 5 部分:存储过程、Python 编程

    调用数据库存储过程及其他感兴趣的高级 Python 编程功能. 2010 年 3 月发布 对于涉及数据库的软件开发来说,有两种主流开发方法:一种是在应用程序中(对于三层体系结构,也可以是在中间件中)实 ...

  5. 谈谈python中的 lambda

    最近刚开始学习python,然后要加几个python的群去学习学习,但是呢有个群的申请栏要求写一个用lambda求1-100的和.....然后悲剧的就是不会啊....然后就没有然后了... 所以去网上 ...

  6. norflash移植及uboot 保存环境变量实验

    一.实验环境 实验板:TQ2440开发板 SDRAM:64M norflash:EN29LV160AB(2M) nandflash:(256M) 二.移植 本文不详谈从smdk2410移植到TQ244 ...

  7. iOS:图像和点击事件

    问题:如何区分点的是哪张图片? // // main.m // Hello // // Created by lishujun on 14-8-28. // Copyright (c) 2014年 l ...

  8. 字符串搜索算法Boyer-Moore

    整理日: 2015年2月16日 1. 主要特征 假设文本串text长度为n,模式串pattern长度为m,BM算法的主要特征为: 从右往左进行比较匹配(一般的字符串搜索算法如KMP都是从从左往右进行匹 ...

  9. Python/Keras如何将给定的数据集打乱

    给定数据集data,数据集对应的标签label index = [i for i in range(len(data))] random.shuffle(index) data = data[inde ...

  10. PyCharm如何设置显示行号?

    File->setting->Editor->General->Appearance,勾选Show line numbers