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. Autofac IContainer 测试

    using Autofac; using System; using System.Collections.Generic; using System.Linq; using System.Text; ...

  2. EF事务

    var db = this.UnitOfWork as CodeFirstDbContext; using (var tan = db.Database.BeginTransaction()) { t ...

  3. html 表格head头部不动 body部分滚动,每格宽同内容增加

    如下图同Excel表格首行固定: <style> .table{ width: 100%; border-collapse:collapse; border-spacing:0;} .ta ...

  4. CO-类的本质、description方法

    类的本质 1. 类也是个对象 其实类也是一个对象,是Class类型的对象,简称“类对象” Class类型的定义 typedef struct objc_class  *Class; 类名就代表着类对象 ...

  5. Yii2 执行流程

    原文地址: http://www.cnblogs.com/cresuccess/p/4874330.html

  6. p命名空间的使用(不推荐用)

    xmlns:p="http://www.springframework.org/schema/p" p:没有xsd文件,直接加上面那句就好了 <!-- singleton和p ...

  7. C# 常用分页

    var num = TCalcPager.CalcPageCount(addList.Count, TDefautValue.PageSize); ; i < num; i++) { var r ...

  8. Linux关于vm虚拟机复制后无法启动网卡

    1.一个月前由于自己一直在开发PHP站点,所以把Linux抛出去很长时间没有碰,最近几天把Linux的一些捡起来, 但在我设置vm虚拟机由于在家里做的实验未做完,复制到U盘想到公司接着做没成像,系统是 ...

  9. PostgreSQL表空间、数据库、模式、表、用户/角色之间的关系

    看PostgreSQL9的官方文档,我越看越迷糊,这表空间,数据库,模式,表,用户,角色之间的关系怎么在PostgreSQL里这么混乱呢?经过中午的一个小实验,我逐渐理清了个中来龙去脉.下面我来还原我 ...

  10. [转载]JavaScript内存分析

    https://github.com/CN-Chrome-DevTools/CN-Chrome-DevTools/blob/master/md/Performance-Profiling/javasc ...