[LeetCode][Java] Permutations

题目:

Given a collection of 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].

题意:

给定一组数字,返回所有的可能的组合。

比如:

[1,2,3]
有下面的组合:

[1,2,3][1,3,2][2,1,3][2,3,1][3,1,2],
and [3,2,1].

算法分析:

建立树,并进行递归 参考博客:http://blog.csdn.net/tuantuanls/article/details/8717262 思路二

建立一棵树,比如说:

对于第k层节点来说,就是交换固定了前面 k-1 位,然后分别 swap(k,k), swap(k, k+1) , swap(k, k+2) ...

例如上图中的第三层,固定了第一位(即2),然后分别交换第1,1位,1,2位,1,3位

AC代码:

public class Solution
{
    public ArrayList<ArrayList<Integer>> permute(int[] num)
    {
    	ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
    	permute(num, 0, result);
    	return result;
    }
    void permute(int[] num, int start, ArrayList<ArrayList<Integer>> result)
    {
    	if (start >= num.length) //终止条件,递归到末尾节点是,将数组转化为链表
    	{
    		ArrayList<Integer> item = convertArrayToList(num);
    		result.add(item);
    	}
    	for (int j = start; j <= num.length - 1; j++)
    	{
    		swap(num, start, j);//交换
    		permute(num, start + 1, result);//交换后子代递归
    		swap(num, start, j);//恢复到交换前的初始状态,以便于得出下一次的交换结果
    	}
    }
    private ArrayList<Integer> convertArrayToList(int[] num) //数组变链表
    {
    	ArrayList<Integer> item = new ArrayList<Integer>();
    	for (int h = 0; h < num.length; h++)
    		item.add(num[h]);
    	return item;
    }
    private void swap(int[] a, int i, int j) //交换
    {
    	int temp = a[i];
    	a[i] = a[j];
    	a[j] = temp;
    }
}

版权声明:本文为博主原创文章,转载注明出处

时间: 2024-11-12 13:39:50

[LeetCode][Java] Permutations的相关文章

Permutations II leetcode java

题目: Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1], and [2,1,1]. 题解: 这道题跟Permutaitons没啥大的区别,就是结果去重. 我之前也有写过去重的两个方法: 一

Permutations leetcode java

题目: Given a collection of 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]. 题解: 这道题就用循环递归解决子问题. 因为求所有组合,这就意味着不能重复使用元素,要用visited数组. 有因为是所有可能的组合,所以

Spiral Matrix leetcode java

题目: Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example, Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You should return [1,2,3,6,9,8,7,4,5]. 题解: 这道题是实现题. 考虑2个初始

Pascal&#39;s Triangle II Leetcode java

题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? 题解: 为了达到O(k)的空间复杂度要求,那么就要从右向左生成结果.相当于你提前把上一行的计算出来,当前行就可以用上一次计算出的结果计算了

Spiral Matrix II leetcode java

题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 题解:这道题跟Spiral Matrix想法也是类似的,就是依照矩阵从外圈到内圈建立

Pascal&#39;s Triangle leetcode java(杨辉三角)

题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 题解:既然讲到了Pascal‘s Triangle,即杨辉三角.那么就先去Wikipedia上面复习一下杨辉三角吧:”杨辉三角形,又称賈憲三角形.帕斯卡三角形.海亚姆三角形,是二项式係數在的

Triangle leetcode java

题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4,1,8,3] ] The minimum path sum from top to bottom is 1

【LeetCode】Permutations

Given a collection of 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 class Solution { public ArrayList<ArrayList<Integer>> permute

[Leetcode][JAVA] Pascal&#39;s Triangle I, II

Pascal's Triangle: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 已知行数生成帕斯卡三角.实际上只要有第i层,那么就能生成第i+1层.每次新生成的层加入最终集合中即可. 1 public List<List<Integer&g