Leetcode Permutation 问题

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 replacement must be in-place, do not allocate extra memory.

Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.

1,2,3 → 1,3,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1

[code]

public class Solution {
    public void nextPermutation(int[] num) {
        if(num==null || num.length<2 )return;
        for(int i=num.length-2;i>=0;i--)
        {
            if(num[i]>=num[num.length-1])
            {
                int temp=num[i];
                for(int k=i;k<=num.length-2;k++)num[k]=num[k+1];
                num[num.length-1]=temp;
            }
            else
            {
                int k=num.length-1;
                while( k>i && num[k]>num[i])k--;
                swap(num, i, k+1);
                return;
            }
        }
    }
    void swap(int []a, int i ,int j)
    {
        int t=a[i];
        a[i]=a[j];
        a[j]=t;
    }
}

Permutation sequence

The set [1,2,3,…,n] contains a total of n! unique permutations.

By listing and labeling all of the permutations in order,
We get the following sequence (ie, for n = 3):

“123”
“132”
“213”
“231”
“312”
“321”

Given n and k, return the kth permutation sequence.
Note: Given n will be between 1 and 9 inclusive.

[code]

public class Solution {
    public String getPermutation(int n, int k) {
        if(k<1)return null;
        int num[]=new int [n];
        for(int i=0;i<n;i++)num[i]=i+1;
        for(int i=0;i<k-1;i++)nextPermutation(num);
        StringBuffer sb=new StringBuffer();
        for(int i=0;i<n;i++)sb.append((char)('0'+num[i]));
        return sb.toString();
    }
    /**以下省略
}

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].

[code]

public class Solution {
    public List<List<Integer>> permuteUnique(int[] num) {
        List<List<Integer>> list=new ArrayList<List<Integer>>();
        if(num==null || num.length==0)return list;
        return helper(num, 0);
    }

    List<List<Integer>> helper(int[]num, int start)
    {
        List<List<Integer>> r= new ArrayList<List<Integer>>();
        if(start==num.length-1)
        {
            ArrayList<Integer> one=new ArrayList<Integer>();
            one.add(num[start]);
            r.add(one);
            return r;
        }
        HashSet<Integer> set=new HashSet<Integer>();
        for(int i=start;i<=num.length-1;i++)
        {
            if(set.contains(num[i]))continue;
            swap(num, start, i);
            List<List<Integer>> list=helper(num, start+1);
            for(List<Integer> li:list)
            {
                li.add(0, num[start]);
                r.add(li);
            }
            swap(num, start, i);
            set.add(num[i]);
        }
        return r;
    }

    void swap(int[]a, int i, int j)
    {
        int t=a[i];
        a[i]=a[j];
        a[j]=t;
    }
}

[Thoughts]

  • 方法1:用nextPermutation
  • 方法2:见code above. 用到了一个brute force中常用的trick,“以x开头的所有情况”. 不遗漏不重复
  • 注意考虑duplicate. (hashset)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值