let 46 Permutations

本文详细介绍了如何使用排列算法生成一组不重复数字的所有可能排列,并提供了具体的实现代码。文章首先解释了排列的基本概念,随后展示了如何通过查找可以交换的位置来生成下一个排列,最终实现了完整的排列生成过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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],
  [3,2,1]
]

主题思想: 就是排列
排列算法,已经知道了,这里只需要注意处理下,什么时候到达边界,需要停止就好了。

这道题的收获, 就是无法把 一个 int[] 通过Arrays.asList() 转化为List<Integer> 只能通过先创建List<Integer> 然后一个一个添加的方式创建

先贴下根据一个数列生成下一个最小的permutation的算法,
从后往前找,找到最先出现的一个i,j 是nums[i]

 //return false if has next 
    public boolean nextPermute(List<List<Integer>> ans,int [] nums){

        int n=nums.length;
        int i=0,j=0;
        //the flag which means whether has next permuate
        boolean flag=true;
        // find the index i is the first time where nums[i]<nums[j]
        for(j=n-1;j>=1;j--){
            i=j-1;
            if(nums[i]<nums[j]){
                flag=false;
              break;  
            } 
        }
        if(flag) return flag;
        //find the index j where nums[j] > nums[i];
        for(j=n-1;j>i;j--){
            if(nums[j]>nums[i]) break;
        }
        //swap i,j
        swap(nums,i,j);

        //reverse from the i+1 to the end
        reverse(nums,i+1,n-1);
        List<Integer> tmp=new ArrayList<Integer>();
        for(int num: nums)  tmp.add(num);
        ans.add(tmp);
        return flag;
    }
    public void swap(int[] nums,int i,int j){
        int t=nums[i];
        nums[i]=nums[j];
        nums[j]=t;
        return ;
    }
    public void reverse(int []nums,int low,int hi){
        if(hi<=low) return ;
        while(low<hi){
            swap(nums,low,hi);
            low++;
            hi--;
        }
        return ;
    }

最后AC代码:

class Solution {
    public List<List<Integer>> permute(int[] nums) {

        List<List<Integer>> ans=new ArrayList<List<Integer>>();
        if(nums==null||nums.length<1) return ans;

        Arrays.sort(nums);
        List<Integer> tmp=new ArrayList<Integer>();
        for(int i: nums) tmp.add(i);
        ans.add(tmp);
        int n=nums.length;
        if(n==1) return ans;

        boolean flag=false;
        while(!flag){

            flag=nextPermute(ans,nums);
        }
        return ans;

        //
    }
    //return false if has next 
    public boolean nextPermute(List<List<Integer>> ans,int [] nums){

        int n=nums.length;
        int i=0,j=0;
        //the flag which means whether has next permuate
        boolean flag=true;
        // find the index i is the first time where nums[i]<nums[j]
        for(j=n-1;j>=1;j--){
            i=j-1;
            if(nums[i]<nums[j]){
                flag=false;
              break;  
            } 
        }
        if(flag) return flag;
        //find the index j where nums[j] > nums[i];
        for(j=n-1;j>i;j--){
            if(nums[j]>nums[i]) break;
        }
        //swap i,j
        swap(nums,i,j);

        //reverse from the i+1 to the end
        reverse(nums,i+1,n-1);
        List<Integer> tmp=new ArrayList<Integer>();
        for(int num: nums)  tmp.add(num);
        ans.add(tmp);
        return flag;
    }
    public void swap(int[] nums,int i,int j){
        int t=nums[i];
        nums[i]=nums[j];
        nums[j]=t;
        return ;
    }
    public void reverse(int []nums,int low,int hi){
        if(hi<=low) return ;
        while(low<hi){
            swap(nums,low,hi);
            low++;
            hi--;
        }
        return ;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值