1、例题--排列 Permutation 
Given a collection of distinct numbers, return all possible permutations.
For example,
[1,2,3] have the following permutations:
[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].
public List<List<Integer>> permute(int[] nums) {
// Implement this method.
}

1)边界条件:组合问题,输入为空,输出为空;其他情况都是正常的。

2)思路:第一个位置先选一个数,第二位置从剩下的两个数里面选一个数,第三个位置只能选最后一个数。

那么N个数的Level-N问题这样化简:第一个位置从N个数里面选择一个,剩余N-1位置和N-1个数,生成一个Level-(N-1)的问题,形成递归。

3)代码实现

public List<List<Integer>> permute(int[] nums) {
  List<List<Integer>> numList = new ArrayList<Integer>(); ///错误的new ArrayList<ArrayList<Integer>>()

  Arrays.sort(nums);//排序有什么用?跟大小没有关系.. 有重复数字的情况会有用。

  for (int i = 0 ; i < nums.length; i++) {

    numList.add(nums[i]);  ///将数组转换为Arraylist,方便后面操作

  }

   return permutes(new ArrayList<Integer>(); numList);

}

  /*先实现递归函数主体*/

  public List<List<Integer>> permutes(ArrayList<Integer> cur, ArrayList<Integer> nums) {

    List<List<Integer>> results = new ArrayList<ArrayList<Integer>>();

    if (0 == nums.size()) {

      ///ArrayList<Integer> result = new ArrayList<>(cur);  ///不需要这一行,因为上一层函数已经申请了新的内存空间

      results.add(result);

      return results;

    }

    for (int i = 0; i < nums.size(); i++) {

      List<Integer> newCur = new ArrayList<>(cur);

      newCur.add(nums.get(i));

      List<Integer> newNum = new ArrayList<>(nums);

      newNums.remove(i);

         result.addAll(permutes(newCur, newNum));

    }

    return results;

  }

4)时间复杂度:O(n!);空间复杂度:O(n!)---空间复杂度似乎不准

5)其他解法

a)

public List<List<Integer>> permute(int[] nums) {
  List<List<Integer>> numList = new List<Integer>();

  for (int i = 0 ; i < nums.length; i++) {

    numList.add(nums[i]);

  }

   return permutes(new List<Integer>(); numList);

}

public List<List<Integer>> permutes(List<Integer> cur, List<Integer> num) {

  List<List<Integer>> results = new ArrayList<Integer>();

  if (0 == num.size()) {

    List<Integer> result = new ArrayList<Integer>(cur); //这里申请新内存保持结果,因为上层函数没有申请,只有一个cur在传递。

    results.add(result);///可以简化一下 results.add(new ArrayList<Integer>(cur))

    return results;

  }

  for (int i = 0; i < num.size(); i++) {

    cur.add(num.get(i));

    num.remove(i);

    results.addAll(permutes(cur, num));

    num.add(cur.get(cur,size() - 1));///没有申请新内存,所以要恢复现场

    cur.remove(cur.size() - 1);

  }

  return results;

}

b)

public ArrayList<ArrayList<Integer>> permute(int[] num) {

  ArrayList<ArrayList<Integer>> results = new ArrayList<Integer>();

  Arrays.sort(nums);

  return permutes(new ArrayList<Integer>(), nums);

}

public ArrayList<ArrayList<Integer>> permutes(ArrayList<Integer> cur, int[] nums) {

  ArrayList<ArrayList<Integer>> results = new ArrayList<Integer>();

  if (cur.size() == nums.length) {

    ArrayList<Integer> result = new ArrayList<Integer>(cur);

    results.add(result); ///可以简化一下 results.add(new ArrayList<Integer>(cur))

    return results;

  }

  for (int i = 0; i < nums.length; i++) {

    if (!cur.contains(nums[i])) {

      cur.add(num[i]);

      results.addAll(permutes(cur, nums));

      cur.remove(cur.size() - 1);   

    }

  }

}

c)Use the num array to serve as the "cur"

[0..index-1] means we have already set the first index
numbers
[index..length-1] are the numbers that we haven't set
Example: [4, 3, 5, 8, 1]
choose 8, then swap (4, 8) => [8, 3, 5, 4, 1]
choose 3, do nothing => [8, 3, 5, 4, 1]
choose 1, then swap (5, 1) => [8, 3, 1, 4, 5]
choose 5, then swap (4, 5) => [8, 3, 1, 5, 4]
choose 4, do nothing, => [8, 3, 1, 5, 4]

public ArrayList<ArrayList<Integer>> permute(int[] nums) {

  ArrayList<ArrayList<Integer>> results = new ArrayList<Integer>();

  Arrays.sort(nums);

  permutes(results, nums, 0);

  return results;

}

public void permutes(ArrayList<ArrayList<Integer>> results, int[] nums, int index) {

  if (index == nums.length) {

    ArrayList<Integer> result = new ArrayList<>();
    for (int i = 0; i < nums.length; i++) {

      result.add(nums[i]);

    }

    results.add(result);

    return;

  }

  for (int i = index; i < nums.length; i++) {

    swap(nums, index, i);

    permutes(results, nums, index + 1);

    swap(nums, i, index);    

  }

  return;

}

public void swap(int[] nums, int i, int j)

2、组合--Combination

Given a collection of distinct numbers, return all possible combinations.
For example,
[2, 6, 8] have the following permutations:
[], [2], [6], [8], [2, 6], [2, 8], [6, 8], [2, 6, 8].
public List<List<Integer>> combine(int[] nums) {
// Implement this method.
}

1)边界条件:空集合;输入为空能在正常流程里面处理吗?

2)思路:对于第一个数2有两种情况,选择或者不选择;然后再对[6,8]进行组合。那么对于Level-N的问题,先对第一个数进行选择/不选择,然后对剩余N-1个数进行组合,这样就化为Level-(N-1),形成了递归。

3)代码实现:

public List<List<Integer>> combine(int[] nums) {

  return combines(new ArrayList<Integer>(), nums, 0);

}

public List<List<Integer>> combines(List<Integer> cur, int[] nums, int index) { ---把结果当做出参

  List<List<Integer>> results = new ArrayList<>();

  if (index == nums.length) {

    results.add(new ArrayList<Integer>(cur)); //上一层没有申请内存,这一层申请

    return results;  

  }

  results.addAll(combines(cur, nums, index + 1));

  cur.add(nums[index]);

  results.addAll(combines(cur, nums, index + 1));

  cur.remove(cur.size() - 1); //恢复现场

}

4)时间复杂度:O(2^n);空间复杂度:O(2^n)?

其他解法:把结果当做入参

public List<List<Integer>> combine(int[] nums) {

  List<List<Integer>> results = new ArrayList<>();

  combines(results, new ArrayList<Integer>(), 0);

  return results;

}

public void combines(List<List<Integer>> results, List<Integer> cur, int[] nums, int index) {

  if (index == nums.length) {

    results.add(cur);//直接添加,上一层已经申请了新内存。

    return;

  }

  List<Integer> newCur1 = new ArrayList<>(cur);

  combines(results, newCur1, nums, index + 1);

  List<Integer> newCur2 = new ArrayList<>(cur);//得申请两个新内存,不然也会互相覆盖。申请两个就不用恢复现场了。

  newCur2.add(nums[index]);

  combines(results, newCur2, index + 1);

  return;

}

lecture-7 递归的更多相关文章

  1. [C2P3] Andrew Ng - Machine Learning

    ##Advice for Applying Machine Learning Applying machine learning in practice is not always straightf ...

  2. Cs231n课堂内容记录-Lecture 8 深度学习框架

    Lecture 8  Deep Learning Software 课堂笔记参见:https://blog.csdn.net/u012554092/article/details/78159316 今 ...

  3. 【cs224w】Lecture 1 & 2 - 图的性质 及 随机图

    目录 Lecture 1: Introduction Lecture 2: Properties and Random Graph Degree Distribution Path Length Cl ...

  4. .NET 基础 一步步 一幕幕[面向对象之方法、方法的重载、方法的重写、方法的递归]

    方法.方法的重载.方法的重写.方法的递归 方法: 将一堆代码进行重用的一种机制. 语法: [访问修饰符] 返回类型 <方法名>(参数列表){ 方法主体: } 返回值类型:如果不需要写返回值 ...

  5. 算法笔记_013:汉诺塔问题(Java递归法和非递归法)

    目录 1 问题描述 2 解决方案  2.1 递归法 2.2 非递归法 1 问题描述 Simulate the movement of the Towers of Hanoi Puzzle; Bonus ...

  6. Android 算法 关于递归和二分法的小算法

     // 1. 实现一个函数,在一个有序整型数组中二分查找出指定的值,找到则返回该值的位置,找不到返回 -1. package demo; public class Mytest { public st ...

  7. 二叉树的递归实现(java)

    这里演示的二叉树为3层. 递归实现,先构造出一个root节点,先判断左子节点是否为空,为空则构造左子节点,否则进入下一步判断右子节点是否为空,为空则构造右子节点. 利用层数控制迭代次数. 依次递归第二 ...

  8. 递归实现n(经典的8皇后问题)皇后的问题

    问题描述:八皇后问题是一个以国际象棋为背景的问题:如何能够在8×8的国际象棋棋盘上放置八个皇后, 使得任何一个皇后都无法直接吃掉其他的皇后?为了达到此目的,任两个皇后都不能处于同一条横行.纵行或斜线上 ...

  9. C语言用分别用递归和循环求数字的阶乘的方法

    以下代码均为 自己 实现,嘻嘻! 参考文章:http://blog.csdn.net/talk_8/article/details/46289683 循环法 int CalFactorial(int ...

  10. C#递归解决汉诺塔问题(Hanoi)

    using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace MyExamp ...

随机推荐

  1. grep的用法(转)

    grep参数 -c  : 显示匹配的行数(就是显示有多少行匹配了): -n :显示匹配内容所在文档的行号: -i  :匹配时忽略大小写: -s :错误信息不输出: -v :输出不匹配内容: -o : ...

  2. java.util Properties使用记录

    转:http://www.2cto.com/px/201006/47834.html 在java.util 包下面有一个类 Properties,该类主要用于读取以项目的配置文件(以.properti ...

  3. JSR-303规范,Bean Validation

    一: JSR 303是JAVA EE 6中的一项子规范,叫做Bean Validation,官方参考实现是Hibernate Validator,此实现与Hibernate ORM没有任何关系.JSR ...

  4. HeartBleed bug

    前两年的一个严重漏洞,影响很大.出现在openssl 1.0.1和1.0.2 beta(包含1.0.1f和1.0.2beta1).利用了TLS的heartbeat. 简单的说,该漏洞被归为缓冲过度读取 ...

  5. Android中EditTex焦点设置和弹不弹出输入法的问题(转)

    今天编程碰到了一个问题:有一款平板,打开一个有EditText的Activity会默认弹出输入法.为了解决这个问题就深入研究了下android中焦点Focus和弹出输入法的问题.在网上看了些例子都不够 ...

  6. 使用python+requests对接口进行测试

    import unittestimport requestsimport json class Login(unittest.TestCase): def setUp(self): self.base ...

  7. [poj1088]滑雪(二维最长下降子序列)

    解题关键:记忆化搜索 #include<cstdio> #include<cstring> #include<algorithm> #include<cstd ...

  8. 12、geo数据上传

    1.注册一个NCBI账户 注册geo账户(老用户和新用户): https://www.ncbi.nlm.nih.gov/geo/submitter/ 有3个月的时间 GEO DataSets > ...

  9. [CentOS7] 设备与文件名对应表

  10. 动态规划---等和的分隔子集(计蒜课)、从一个小白的角度剖析DP问题

    自己还是太菜了,算法还是很难...这么简单的题目竟然花费了我很多时间...在这里我用一个小白的角度剖析一下这道题目. 晓萌希望将1到N的连续整数组成的集合划分成两个子集合,且保证每个集合的数字和是相等 ...