【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(int[] num) {
ArrayList<ArrayList<Integer>> re =new ArrayList<ArrayList<Integer>>();
if(num.length==0)
return re;
ArrayList<Integer> ai = new ArrayList<Integer>();
ArrayList<ArrayList<Integer>> te =new ArrayList<ArrayList<Integer>>();
ai.add(num[0]);
re.add(ai);
te.add(ai);
if(num.length==1)
return re;
for(int i=1;i<num.length;i++){
int t = num[i];
Iterator<ArrayList<Integer>> it = re.iterator();
te =new ArrayList<ArrayList<Integer>>();
while(it.hasNext()){
ai = it.next();
for(int j=0;j<ai.size();j++){
ai.add(j, t);
ArrayList<Integer> tem = new ArrayList<Integer>();
for(int mm=0;mm<ai.size();mm++)
tem.add(ai.get(mm));
te.add(tem);
ai.remove(j);
}
ArrayList<Integer> ttem = new ArrayList<Integer>();
for(int nn=0;nn<ai.size();nn++)
ttem.add(ai.get(nn));
ttem.add(t);
te.add(ttem);

}
re=te;
te=new ArrayList<ArrayList<Integer>>();
}
return re;

}
}

【LeetCode】Permutations,布布扣,bubuko.com

时间: 2024-10-12 17:58:53

【LeetCode】Permutations的相关文章

【LeetCode】Permutations 解题报告

全排列问题.常用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. [题目] 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

【leetcode】Permutations II (middle)

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]. 思路: 找规律递归 一个数跟自己后面的数字交换如 1 2 2 第一个数字和第2个数字换, 但是不换重复的数字 第一个

【leetcode】Permutations (middle)

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]. 求没有重复数字的全排列 思路:用的标准回溯法,一次AC class Solution { public: vector<vector<int>

【LeetCode】Permutations II

Permutations II 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]. 首先分析一下与Permutations有何差异. 记当前位置为start,当前排列数

【LeetCode】Next Permutation 解题报告

[题目] Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order). The repl

【leetcode】 Permutation Sequence

问题: 对于给定序列1...n,permutations共有 n!个,那么任意给定k,返回第k个permutation.0 < n < 10. 分析: 这个问题要是从最小开始直接到k,估计会超时,受10进制转换为二进制的启发,对于排列,比如 1,2,3 是第一个,那么3!= 6,所以第6个就是3,2,1.也就是说,从开始的最小的序列开始,到最大的序列,就是序列个数的阶乘数.那么在1,3 , 2的时候呢?调整一下,变成2,1,3,就可以继续. 实现: int getFactorial(int n

【leetcode】Generate Parentheses

题目: 给定整数n,返回n对匹配的小括号字符串数组. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" 分析: 这种问题的模式是:1)问题的解有多个 ,2)每个解都是由多个有效的 "步骤" 组成的,3)变更以有解的某个或某些"步骤"

【LeetCode】Implement strStr()

Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack. 标准KMP算法.可参考下文. http://blog.csdn.net/yaochunnian/article/details/7059486 核心思想在于求出模式串前缀与后缀中重复部分,将重复信息保存在n

【LeetCode】Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 ->