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. Class.forName()用法详解

    Class.forName()用法详解 标签: classjvmjdbc数据库documentationjava 2012-03-29 09:39 40414人阅读 评论(8) 收藏 举报  分类: ...

  2. vim tab 中设置title

    在.bashrc添加 export PROMPT_COMMAND='echo -ne "\033]0;your wanted title\007"'

  3. [CentOs]ip操作

    摘要 在虚机里面安装好centos之后,需要知道centos的ip,方便以后连接时使用. 查看ip命令 命令 ifconfig 能查看到信息,说明已经配置过了,如果没配置过,可以通过下面的方式进行配置 ...

  4. Java反射机制(Reflection)

    Java反射机制(Reflection) 一.反射机制是什么 Java反射机制是程序在运行过程中,对于任意一个类都能够知道这个类的所有属性和方法;对于任意一个对象都能够调用它的任意一个方法和属性,这种 ...

  5. clang

    1.安装 clang 可以从官网下载,如果是CentOS 6 系统,也可以在 /etc/yum.repos.d/ 目录下增加一个 epel.repo 文件,内容如下: [epel] name=Extr ...

  6. Cocos2D-X 学习笔记

    1. 3.4final控制台创建项目后,安卓编译会失败,必须手动把cocos/平台/andorid/java/src目录里的文件复制到安卓项目的src文件夹即可 2. 安卓的文件目录与win的略有不同 ...

  7. OC第二节 —— NSString和NSMutableString

    1.为什么需要NSString对象        答:在OC中创建字符串时,一般不使用C的方法,    因为C将字符串作为字符数组,所以在操作时会有很多不方便的地方,    在Cocoa中NSStri ...

  8. Ubuntu 14 编译安装 PHP 5.4.45 + Nginx 1.4.7 + MySQL 5.6.26 笔记

    Ubuntu 14 编译安装 PHP 5.4.45 + Nginx  1.8.0/1.4.7 + MySQL 5.6.26 笔记,主要是给自己的PC机安装,非生产环境! 一.下载必要的源码 1.1.下 ...

  9. PYTHONPATH 可以跨版本 方便使用 (本文为windows方法)转~

    PYTHONPATH是Python搜索路径,默认我们import的模块都会从PYTHONPATH里面寻找. 使用下面的代码可以打印PYTHONPATH: print(os.sys.path) 我的某个 ...

  10. 用hexo书写github.io博客 学习心得 教程

    很久没更新文章了,除了工作忙之外,可能就是自己懒惰了. 最近混迹与github,发现git上写博客也是个很不错的平台. 推荐使用 hexo 模版来书写,毕竟我们重点是写文章,而不是管理,所以有神奇何妨 ...