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. 序列化与反序列化成XML

    http://blog.itpub.net/12639172/viewspace-490786/ 现在XML都普遍的用到了很多地方,它的平台无关.方便.结构化.适用性的特点让人不得不去接受它,在C#中 ...

  2. yum管理

    一.yum发展与作用     在linux系统维护中管理员经常遇到软件包的依赖问题,有时无法解决,比如你在安装库文件时常出现报错问题,说依赖其它软件包.由于这个问题一直困绕linux的广大爱好者,开源 ...

  3. Thinkphp 连接查询的使用

    方法一:使用table()方法 $tables = 'b_order ordert, b_busbid busbid'; $map['busbid.buscompanyid'] = 1; $map[' ...

  4. 3步完成chrome切换搜索引擎

    1.打开chrome://settings/,找到搜索 2.点击“管理搜索引擎”,出现弹窗. 增加搜索引擎,三个文本框分别输入:名称.快捷键.地址 3.在新的选项卡中,输入快捷键(如:github), ...

  5. 我再也不-或许永远不-用zend studio-受够了!

    zend studio背负的东西太多, 想要整合php, js, aptana, emmet, 还要git, 所以显得很累. 不过把这些它整合的东西 都用上后, 用好后, 倒确实是php的开发利器. ...

  6. 修改emlog表字段名称

    在em_twitter表中增加一个字段. ,添加一个字段isImportant alter table em_twitter add isImprotant ) not ; ,把字段isImprota ...

  7. Notepad++的插件

    1.4. Notepad++中常用的插件 1.4.1. 插件管理器: Plugin Manager 插件功能:此插件可以帮你管理插件,包括查看当前已经安装的插件有哪些,以及自动帮你下载相应的插件. 插 ...

  8. List<List<double>> lsls = null; 根据double值来重新排序lsls...

    "确定:Node-data = (7,2).具体是:根据x维上的值将数据排序, 6个数据的中值(所谓中值,即中间大小的值)为7, 所以Node-data域位数据点(,).这样, 该节点的分割 ...

  9. 【Solr】solr的增删改查

    目录 创建工程 增 删 改 查 高量查询 回到顶部 创建工程 普通的java web工程即可,我采用的是spring mvc! 回到顶部 增 @Autowired private SolrServer ...

  10. 【UEditor】 UEditor整合项目上传资源到阿里云服务器

    目录 关于此文 下载源码 JSP代码 Java代码 阿里云jar包引入配置 成功啦! 回到顶部 关于此文 项目中要实现编辑器生成带格式的html文档,存入模板,最后生成html的URL,所以选择了UE ...