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]

Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.

解法思想:递归

代码如下:

public class Solution {
public List<Integer> diffWaysToCompute(String input) {
List<Integer> ret=new LinkedList<Integer>();
int len=input.length();
for(int i=0;i<len;i++){
if(input.charAt(i)=='-'||
input.charAt(i)=='*'||
input.charAt(i)=='+'){
String part1=input.substring(0,i);
String part2=input.substring(i+1);
List<Integer> part1Ret=diffWaysToCompute(part1);
List<Integer> part2Ret=diffWaysToCompute(part2);
for(Integer p1:part1Ret){
for(Integer p2:part2Ret){
int c=0;
switch(input.charAt(i)){
case '+':c=p1+p2;
break;
case '-':c=p1-p2;
break;
case '*': c=p1*p2;
}
ret.add(c);
}
}
}
}
if(ret.size()==0){
ret.add(Integer.valueOf(input));
}
return ret;
}
}

  运行结果:

(medium)LeetCode 241.Different Ways to Add Parentheses的更多相关文章

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

  2. [LeetCode] 241. Different Ways to Add Parentheses 添加括号的不同方式

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

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

  4. [LeetCode#241]Different Ways to Add Parentheses

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

  5. LeetCode 241. Different Ways to Add Parentheses为运算表达式设计优先级 (C++)

    题目: Given a string of numbers and operators, return all possible results from computing all the diff ...

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

  7. 241. Different Ways to Add Parentheses

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

  8. 【LeetCode】241. Different Ways to Add Parentheses

    Different Ways to Add Parentheses Given a string of numbers and operators, return all possible resul ...

  9. LC 241. Different Ways to Add Parentheses

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

随机推荐

  1. Oracle中的不等于号

    今天碰到一个Oracle不等于的问题,最后搜索了一下,发现下面资料,拿来跟大家分享一下   关于Oracle中的不等于号: 在Oracle中, <> != ~= ^= 都是不等于号的意思. ...

  2. Settings.System.getInt获取Setting里的设置信息

    数据库的路径:/data/data/com.android.providers.settings. 获取飞行模式: Settings.System.getInt(mContext.getContent ...

  3. 如何通过源码生成Gatling可执行工具

    其实,这个对于不是很熟系sbt的人来说,或者对scala语言没有什么了解的人,接触Gatling这个开源的性能测试框架,还是有些茫然的. 因为GitHub上提供的Gatling (最新版本:2.2.0 ...

  4. .NET(C#)生成条形码

    using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Secu ...

  5. Servlet Filter 3

    11.MD5加密 /** * 使用md5的算法进行加密 */ public static String md5(String plainText) { byte[] secretBytes = nul ...

  6. python命令行下tab键补全命令

    在python命令行下不能使用tab键将命令进行补全,手动输入又很容易出错. 解决:tab.py #/usr/bin/env python # -*- coding:utf-8 -*- ''' 该模块 ...

  7. IIS SMTP status codes

    Here are the meaning of SMTP status codes. Status Code Description 211 System status, or system help ...

  8. spring+springMVC+mybatis的框架项目基础环境搭建

    上一个项目在后台用到spring+springMVC+mybatis的框架,先新项目初步需求也已经下来,不出意外的话,应该也是用这个框架组合. 虽然在之前activiti相关的学习中所用到的框架也是这 ...

  9. VS 使用Sql Server 数据库增删改查

    /// <summary> /// 执行查询语句,返回DataSet /// </summary> /// <param name="SQLString&quo ...

  10. 黄聪:HtmlAgilityPack,C#实用的HTML解析类 ---- HtmlNode类

    HtmlAgilityPack中的HtmlNode类与XmlNode类差不多,提供的功能也大同小异.下面来看看该类提供功能. 一.静态属性 public static Dictionary<st ...