Description:

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]

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

使用分治递归的方法首先将问题划分成子问题,再对子问题的解进行全排列合并,最终得到问题的解。

深刻理解递归还是很有必要的。

  1. public class Solution {
  2. public List<Integer> diffWaysToCompute(String input) {
  3.  
  4. List<Integer> resList = new ArrayList<Integer>();
  5.  
  6. for(int i=0; i<input.length(); i++) {
  7. char op = input.charAt(i);
  8. if(op == '+' || op == '-' || op == '*') {
  9.  
  10. List<Integer> leftList = diffWaysToCompute(input.substring(0, i));
  11. List<Integer> rightList = diffWaysToCompute(input.substring(i+1));
  12.  
  13. for(int left : leftList) {
  14. for(int right : rightList) {
  15. if(op == '+') {
  16. resList.add(left + right);
  17. }
  18. else if(op == '-') {
  19. resList.add(left - right);
  20. }
  21. else {
  22. resList.add(left * right);
  23. }
  24. }
  25. }
  26.  
  27. }
  28. }
  29.  
  30. if(resList.size() == 0) {
  31. resList.add(Integer.parseInt(input));
  32. }
  33.  
  34. return resList;
  35. }
  36. }

这题竟然没有不是个位数的case。

LeetCode——Submission Details的更多相关文章

  1. leetcode Submission Details

    代码: #include<iostream> #include<vector> using namespace std; struct ListNode { int val; ...

  2. Submission Details [leetcode] 算法的改进

    最先看到这一题,直觉的解法就是len从1到s1.size()-1,递归调用比較s1和s2长度为len的子串是否相等.以及剩余部分是否相等. 将s1分成s1[len + rest],分别比較s2[len ...

  3. 【leetcode】Submission Details

    Given two sentences words1, words2 (each represented as an array of strings), and a list of similar ...

  4. Submission Details

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  5. [LeetCode OJ] Max Points on a Line

    Max Points on a Line Submission Details 27 / 27 test cases passed. Status: Accepted Runtime: 472 ms ...

  6. [leetCode][012] Two Sum (1)

    [题目]: Given an array of integers, find two numbers such that they add up to a specific target number ...

  7. [leetCode][016] Add Two Numbers

    [题目]: You are given two linked lists representing two non-negative numbers. The digits are stored in ...

  8. leetcode 15. 3Sum 二维vector

    传送门 15. 3Sum My Submissions Question Total Accepted: 108534 Total Submissions: 584814 Difficulty: Me ...

  9. [sqoop1.99.6] 基于1.99.6版本的一个小例子

    1.创建mysql数据库.表.以及测试数据mysql> desc test;+-------+-------------+------+-----+---------+------------- ...

随机推荐

  1. dubbo_rpc原理

    alibaba有好几个分布式框架,主要有:进行远程调用(类似于RMI的这种远程调用)的(dubbo.hsf),jms消息服务(napoli.notify),KV数据库(tair)等.  这个框架/工具 ...

  2. Jackson2.1.4 序列化对象时,过滤null的属性 empty的属性 default的属性

    在进行序列化如何过滤为null的属性,empty的属性,或者default的属性. 一.全局注册 objectMapper.setSerializationInclusion(Include.ALWA ...

  3. 第三百一十六节,Django框架,中间件

    第三百一十六节,Django框架,中间件 django 中的中间件(middleware),在django中,中间件其实就是一个类,在请求到来和结束后,django会根据自己的规则在合适的时机执行中间 ...

  4. HTC Desire 816刷机教程(图文)

    HTC Desire 816刷机教程也来了,今天在这里主要是来说说如何刷机的,这个刷机是采用卡刷的方式,也就是利用第三方的recovery来刷入第三方的zip包,因为第三方的zip包都是支持卡刷的,很 ...

  5. Collapsing Margin:外边距叠加

    参考:http://www.smallni.com/collapsing-margin/ http://www.cnblogs.com/v10258/p/3530290.html

  6. perl File::Spec 模块

    File::Spec 模块提供了很多的功能,这里只列举几个常用的函数 rel2abs : 返回一个文件的绝对路径, 常见用法,返回当前运行的perl脚本的绝对路径 代码示例: my $prog = F ...

  7. CentOS定位、查找文件的命令

    定位.查找文件的命令 命令 功能 命令 功能 which 从path中找出文件的位置 find 找出所有符合要求的文件 whereis 找出特定程序的路径 locate 从索引中找出文件位置 9.1 ...

  8. MathType公式波浪线怎么编辑

    数学公式中有很多符号与数学样式,在用手写时是没有问题的,但是很多论文或者期刊中也是需要用到这些符号或者样式的,比如公式波浪线,那么MathType公式波浪线怎么编辑出来呢? 具体操作步骤如下: 1.打 ...

  9. CSS定位背景图片 background-position

    网站的样式的时候经常会发现一种情况,就是在很多background属性里都调用同一张图片,来满足网页各个部分的使用.打开这种图片看一下,会发现这张图片上包含了很多小图片; 又如: 这些小图片就是整图分 ...

  10. sftp,get命令使用*通配符的方式获取批量的文件

    需求描述: 今天在使用sftp进行get文件的时候,有很多文件名类似的文件,以为还是需要一个一个get 后来发现get也可以使用通配符的方式进行匹配获取多个文件,在此记录下 操作过程: 1.通过sft ...