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

  1. ((2-1)-1) = 0
  2. (2-(1-1)) = 2

Output: [0, 2]

Example 2

Input: "2*3-4*5"

  1. (2*(3-(4*5))) = -34
  2. ((2*3)-(4*5)) = -14
  3. ((2*(3-4))*5) = -10
  4. (2*((3-4)*5)) = -10
  5. (((2*3)-4)*5) = 10

Output: [-34, -14, -10, -10, 10]

题目大意:给定一个表达式,让你随便在上面加括号,然后算所有可能的结果。

解题思路:

这题如果顺着题目的意思,自己去加括号然后算表达式的值那就完了,掉坑里出不来了,其实就是分治,举个例子:

对表达式,找到一个运算符,然后计算式子两边的所有可能的值,然后把两边分别乘起来就可以了,递归的计算两边的值。

题目的要求就是计算所有的。

  1. public static List<Integer> diffWaysToCompute(String input) {
  2. List<Integer> res = new ArrayList<>();
  3. if(input==null||input.length()==0){
  4. return res;
  5. }
  6. for(int i=0;i<input.length();i++){
  7. char c = input.charAt(i);
  8. if(!Character.isDigit(c)){
  9. List<Integer> pre = diffWaysToCompute(input.substring(0,i));
  10. List<Integer> post = diffWaysToCompute(input.substring(i+1,input.length()));
  11. for(int f : pre){
  12. for(int l : post){
  13. if(c=='+'){
  14. res.add(f+l);
  15. }else if(c=='-'){
  16. res.add(f-l);
  17. }else if(c=='*'){
  18. res.add(f*l);
  19. }
  20. }
  21. }
  22. }
  23. }
  24. if(res.size()==0){
  25. res.add(Integer.valueOf(input));
  26. }
  27. Collections.sort(res);
  28. return res;
  29. }

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

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

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

  4. 241. Different Ways to Add Parentheses

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

  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. (medium)LeetCode 241.Different Ways to Add Parentheses

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

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

随机推荐

  1. JQuery Datatables(一)

    最近项目中用了Bootstrap的样式风格,控件用了JQuery Datatables,主要有几下几点目标: 实现BootStrap风格的table,使用Ajax获取数据,并有勾选项 可以实现全选,单 ...

  2. [时间操作] C#TimeHelper时间格式化帮助类 (转载)

    点击下载 TimeHelper.rar 主要功能如下 .将时间格式化成 年月日 的形式,如果时间为null,返回当前系统时间 .将时间格式化成 时分秒 的形式,如果时间为null,返回当前系统时间 . ...

  3. 自定义带有图片的PreferenceActivity

    http://my.oschina.net/huangsm/blog/40027 和大家分享一下关于android中PreferenceActivity使用以及为配置信息文件中添加图标的功能,首先给大 ...

  4. C#当中的多线程_线程基础

    前言 最近工作不是很忙,想把买了很久了的<C#多线程编程实战>看完,所以索性把每一章的重点记录一下,方便以后回忆. 第1章 线程基础 1.创建一个线程 using System; usin ...

  5. Android增加监听的三种实现方式

    在Android中,为一个按钮增加监听的方式有五种 1.匿名内部类 @Override protected void onCreate(Bundle savedInstanceState) { sup ...

  6. C++专题 - Qt是什么

    Qt是一个1991年由奇趣科技开发的跨平台C++图形用户界面应用程序开发框架.它既可以开发GUI程式,也可用于开发非GUI程式,比如控制台工具和服务器.Qt是面向对象的框架,使用特殊的代码生成扩展(称 ...

  7. POJ 1564(HDU 1258 ZOJ 1711) Sum It Up(DFS)

    题目链接:http://poj.org/problem?id=1564 题目大意:给定一个整数t,和n个元素组成的集合.求能否用该集合中的元素和表示该整数,如果可以输出所有可行解.1<=n< ...

  8. mysql主要应用场景 转载

    前言 据说目前MySQL用户已经达千万级别了,其中不乏企业级用户.可以说是目前最为流行的开源数据库管理系统软件了.任何产品都不可能是万能的,也不可能适用于所有的应用场景.那么MySQL到底在什么场景下 ...

  9. Qt XML的使用

    Qt中对于XML文件的写入有两种方式,一个是使用QXmlStreamWriter,另一个则为使用Dom.stream流的形式相对来说更加灵活,而且适合处理大文件.Dom方式由于是将内容加载到了内存中进 ...

  10. Balsamiq Mockups

    Balsamiq Mockups完全手册 2010.03.18 发布 48,066 浏览 什么是Balsamiq Mockups Balsamiq Mockups出自加利福尼亚州的Balsamiq工作 ...