LeetCode:全排列【46】

题目描述

给定一个没有重复数字的序列,返回其所有可能的全排列。

示例:

输入: [1,2,3]
输出:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]

题目分析

  首先题目给了一个没有重复数字的序列,它的全排列也一定不含重复数字。我们采用回溯框架法快速解题。

  我们就简单思考一个问题,每个排列的第一个元素是如何生成的!

我们从左往右,首先我们将a加入tmpList(临时存储排列的线性表)中,此后再从它下一个位置开始找第二个、第三个数字,最后生成排列存储在结果中。我们再将1作为第一个元素开始处理后,赶紧把他删了,再将b加入到tmpList中,此后再从它下一个位置开始找第二个、第三个数字,最后生成排列存储在结果中....

  也就是说处理第i个位置的元素时,就要考虑到i位置的所有取值,我们在处理为a的元素后,赶紧把他删了处理为b的元素.....这样第i个位置就有所有的情况了

  

  知道这个以后,我们就可以得到在一个数组中选出所有N个数的组合。

  我觉得这一段讲的并不是特别清楚,我只是在讲某一层的元素要如何处理,该层要考虑所有元素,就在处理后某个元素后,再把该层腾空,让给其他元素。

Java题解

public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> list = new ArrayList<>();
// Arrays.sort(nums); // not necessary
backtrack(list, new ArrayList<>(), nums);
return list;
} private void backtrack(List<List<Integer>> list, List<Integer> tempList, int [] nums){
if(tempList.size() == nums.length){
list.add(new ArrayList<>(tempList));
} else{
for(int i = 0; i < nums.length; i++){
if(tempList.contains(nums[i])) continue; // element already exists, skip
tempList.add(nums[i]);
backtrack(list, tempList, nums);
tempList.remove(tempList.size() - 1);
}
}
}

LeetCode:全排列【46】的更多相关文章

  1. LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2

    题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...

  2. Leetcode题目46.全排列(回溯+深度优先遍历+状态重置-中等)

    题目描述: 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], ...

  3. LeetCode(46):全排列

    Medium! 题目描述: 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [ ...

  4. Leetcode 全排列专题(更新ing)

    总览 涉及到的题目有 题号 名字 难度 Leetcode 60 第k个排列 中等 Leetcode 46 全排列 中等 待更新...... Leetcode 46 全排列 题目 基础题 给定一个 没有 ...

  5. [LeetCode] “全排列”问题系列(二) - 基于全排列本身的问题,例题: Next Permutation , Permutation Sequence

    一.开篇 既上一篇<交换法生成全排列及其应用> 后,这里讲的是基于全排列 (Permutation)本身的一些问题,包括:求下一个全排列(Next Permutation):求指定位置的全 ...

  6. [Leetcode][Python]46: Permutations

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 46: Permutationshttps://leetcode.com/pr ...

  7. 【一天一道LeetCode】#46. Permutations

    一天一道LeetCode系列 (一)题目 Given a collection of distinct numbers, return all possible permutations. For e ...

  8. [LeetCode] “全排列”问题系列(一) - 用交换元素法生成全排列及其应用,例题: Permutations I 和 II, N-Queens I 和 II,数独问题

    一.开篇 Permutation,排列问题.这篇博文以几道LeetCode的题目和引用剑指offer上的一道例题入手,小谈一下这种类型题目的解法. 二.上手 最典型的permutation题目是这样的 ...

  9. 【LeetCode】46. Permutations 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 方法一:库函数 方法二:递归 方法三:回溯法 日期 题目地址:h ...

  10. LeetCode 【46. Permutations】

    Given a collection of distinct numbers, return all possible permutations. For example,[1,2,3] have t ...

随机推荐

  1. 141. Linked List Cycle【easy】

    141. Linked List Cycle[easy] Given a linked list, determine if it has a cycle in it. Follow up:Can y ...

  2. python-获取本机mac地址

    #!/usr/bin/env python #-*- coding:utf-8 -*- ############################ #File Name: getmac.py #Auth ...

  3. waterfall.js

    jq-waterfall是一款仿Pinterest网站的响应式无限动态加载图片瀑布流特效jQuery插件.该瀑布流特效使用ajax调用来动态加载图片,达到无限加载的效果.它使用简单,兼容性好,值得推荐 ...

  4. codeblocks设置当前行高亮

    默认是不开启当前行高亮的. 如果想打开,选择:Settings>Editor>Editor Settings>Other options> 勾选Highlight line u ...

  5. Unity学习笔记 - UI System(一)

    转载请注明出处: EnigmaJJ http://www.cnblogs.com/twjcnblog/p/5850648.html 术语 Canvas是Unity的原生组件,Unity的渲染系统使用C ...

  6. Yarn源码分析之MRAppMaster上MapReduce作业处理总流程(二)

    本文继<Yarn源码分析之MRAppMaster上MapReduce作业处理总流程(一)>,接着讲述MapReduce作业在MRAppMaster上处理总流程,继上篇讲到作业初始化之后的作 ...

  7. spark集群模式

    1.配置集群主机免登陆,参考http://www.cnblogs.com/puroc/p/5996730.html2.修改/etc/hosts文件,增加所有主机名和IP的对应关系 3.修改spark- ...

  8. iostat -d -k -x 1 10

    iostat命令详解 转载 2011年08月03日 14:13:58 标签:磁盘 /扩展 /user 99809 iostat iostat用于输出CPU和磁盘I/O相关的统计信息. 命令格式: io ...

  9. 通信协议之广播---recvfrom 放回客户端的ip地址第一次全为0.0.0.0

    遇到一个很恶心的问题. while(1) { recvfrom(sockfd, readbuff, UDP_CMD_BUFF_LENGTH - 1, 0, (struct sockaddr *)&am ...

  10. (phpQuery)对网站产品信息采集代码的优化

    a.要采集的源链接: http://www.prospecbio.com/Recombinant_Proteins/ b.具体要求: