- 浏览: 173381 次
- 性别:
- 来自: 济南
文章分类
最新评论
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].
全排列的问题,同样用回溯法,回溯的边界条件我们可以通过每个结果的长度来判断,当结果的长度为给定数组的长度时,我们就开始回溯。往下寻找的时候,开始位置我们都从0开始,但这样会有重复的元素被记录,所以我们要加一个判断,判断当前元素是否已经被记录。代码如下:
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].
全排列的问题,同样用回溯法,回溯的边界条件我们可以通过每个结果的长度来判断,当结果的长度为给定数组的长度时,我们就开始回溯。往下寻找的时候,开始位置我们都从0开始,但这样会有重复的元素被记录,所以我们要加一个判断,判断当前元素是否已经被记录。代码如下:
public class Solution { public List<List<Integer>> permute(int[] nums) { LinkedList<Integer> list = new LinkedList<Integer>(); LinkedList<List<Integer>> llist = new LinkedList<List<Integer>>(); if(nums == null || nums.length == 0) return llist; getPermute(0, nums, list, llist); return llist; } public void getPermute(int start, int[] nums, LinkedList<Integer> list, LinkedList<List<Integer>> llist) { if(list.size() == nums.length) { llist.add(new LinkedList<Integer>(list)); } for(int i = start; i < nums.length; i++) { if(!list.contains(nums[i])) { list.add(nums[i]); getPermute(0, nums, list, llist); list.removeLast(); } } } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 227Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 226You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 342Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 333Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 451Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 511Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 430Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 619Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 428The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 387Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 517Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 531Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 371All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 861Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 883Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 555Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 601Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 764Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 735You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 629For a undirected graph with tre ...
相关推荐
Richard P. Stanley的经典文章A Survey of Alternating Permutations,供有需要者下载
Permutations are ubiquitous in many real-world problems, such as voting, ranking, and data association. Representing uncertainty over permutations is challenging, since there are n! possibilities, and...
C#,排列组合的堆生成法(Heap’s Algorithm for generating permutations)算法与源代码 排列组合的堆生成法 堆生成算法用于生成n个对象的所有组合。其思想是通过选择一对要交换的元素,在不干扰其他n-2元素的情况...
Chapter1PermutationsandCombinations排列和组合.pdf
安装$ npm install --save permutations-count 用法 var permutationsCount = require ( 'permutations-count' ) ;var numPermutations = permutationsCount ( 12 , 3 ) ;console . log ( numPermutations ) ; // =>...
大师Donald E. Knuth(汉名高德纳)的著作,计算机程序设计与艺术第四卷3册:生成所有组合和分划Generating All Combinations and Permutations(中英)
信息安全_数据安全_Lossy_Trapdoor_Permutations_with 事件检测 定向攻击 常规渗透 移动安全 事件检测
资源分类:Python库 所属语言:Python 资源全名:email_permutations-0.2.0-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] Output: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ] 二.解题思路 主要是有两种...
大师Donald E. Knuth(汉名高德纳)的著作,计算机程序设计与艺术第四卷2册:生成所有元组和排列Generating All Tuples and Permutations(中英)
数字排列重复数排列Расчётколичествастроквфайлепроизвёлкомандой:.. \ permutations.txt“ |测量对象-线ВWindouse 标题:行字字符属性30240
排列
permutations ( 'abc' ) // [ 'abc', 'acb', 'bac', 'bca', 'cab', 'cba' ]permutations ( 'ab' ) // [ 'ab', 'ba']permutations ( 'aa' ) // [ 'aa', 'aa' ]permutations ( 'aa' , { unique : true } ) // [ 'aa' ]...
D型排列上的超越数,赵飞燕,,本文主要研究了B型超越数,稳定点和圈在D型排列上的分布。我们得到了带符号的超越数在D型排列和D型错排上的递推关系和闭公式。
matlab导入excel代码utl_permutations_of_pairs_using_unique_values_by_group 按组使用唯一值的对的排列。 关键字:sas sql join合并大数据分析宏oracle teradata mysql sas社区stackoverflow statistics人工智慧AI...
本章是Gilbert Strang的MIT线性代数Linear Algebra公开课中【第五章 转置-置换-向量空间(lecture 5 Transposes, Permutations, Vector Spaces)】的笔记,参考他在 MIT Linear Algebra课程网站上公开分享的 lecture...
vs-recursion-and-permutations
cyclic_permutations_and_prime_numbers