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]

分析:

加了括号以后,每一个operator在某种情况下都会被最后执行。

    public List<Integer> diffWaysToCompute(String input) {
List<Integer> list = new ArrayList<Integer>();
for (int i = ; i < input.length(); i++) {
if (input.charAt(i) == '+' || input.charAt(i) == '-' || input.charAt(i) == '*') {
List<Integer> left = diffWaysToCompute(input.substring(, i));
List<Integer> right = diffWaysToCompute(input.substring(i + ));
for (int l : left) {
for (int r : right) {
if (input.charAt(i) == '+') {
list.add(l + r);
} else if (input.charAt(i) == '-') {
list.add(l - r);
} else if (input.charAt(i) == '*') {
list.add(l * r);
}
}
}
}
}
if (list.size() == ) {
list.add(Integer.parseInt(input));
}
return list;
}

Different Ways to Add Parentheses的更多相关文章

  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. 241. Different Ways to Add Parentheses

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

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

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

  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. 241. Different Ways to Add Parentheses——本质:DFS

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

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

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

随机推荐

  1. xss漏洞挖掘小结

    xss漏洞挖掘小结 最近,在挖掘xss的漏洞,感觉xss真的不是想象的那样简单,难怪会成为一类漏洞,我们从防的角度来讲讲xss漏洞的挖掘方法: 1.过滤 一般服务器端都是采用这种方式来防御xss攻击, ...

  2. SCWS分词扩展在UNIX/LINUX下的安装方法

    <?php/** * 中文分词处理方法 *+--------------------------------- * @param stirng  $string 要处理的字符串 * @param ...

  3. Python之路【番外篇】回顾&类的静态字段

    回顾 回顾:字符串.列表.字典的修改关于内存的情况 一.字符串 str1 = 'luotianshuai' str2 = str1 print id(str1) print id(str2) prin ...

  4. jquery报错Uncaught ReferenceError: $ is not defined

  5. SQL Server2008 with(lock)用法

    参考资料: WITH (NOLOCK) 在sqlserver 中with(nolock)详解

  6. CF449B Jzzhu and Cities (最短路)

    CF449B CF450D http://codeforces.com/contest/450/problem/D http://codeforces.com/contest/449/problem/ ...

  7. 字符编码-UNICODE,GBK,UTF-8区别【转转】

    字符编码介绍及不同编码区别 今天看到这篇关于字符编码的文章,抑制不住喜悦(总结的好详细)所以转到这里来.转自:祥龙之子http://www.cnblogs.com/cy163/archive/2007 ...

  8. SqlServer代理执行[分发清除: distribution] 无法删除快照文件

    每天偶尔检查数据库作业是否失败,发现有错误 select top 10 job_id,run_date,run_time,run_duration,step_name,message from  ms ...

  9. (2)apply函数及其源码

      本文原创,转载请注明出处,本人Q1273314690(交流学习) 总结: 就是MARGIN决定了你的FUN调用几次,每次传递给你的是什么维度的内容,而...是传递给FUN的(每次调用的时候都会被传 ...

  10. ios如何普安短图片类型

    很多时候需要知道服务器返回的图片是.png还是.jpg或者是.git, 两种方式 1,获取扩展名 //图片    NSString *image = @"4351141241.GIT&quo ...