当前位置: 首页> 教育> 锐评 > day-45 全排列 II

day-45 全排列 II

时间:2025/7/13 3:52:39来源:https://blog.csdn.net/qq_53568730/article/details/141758787 浏览次数:0次

在这里插入图片描述
思路
与上一题思路相同,代码也基本一致,只是需要全排列不重复

解题过程
可以利用Arrays.sort()函数将nums数组进行排序,这样相同的全排列数字的位置也会相同,可以利用List的contains()函数进行判断,如果不重复则加入答案

Code

class Solution {public List<List<Integer>> list=new ArrayList<>();public int len;public int choose[];public List<List<Integer>> permuteUnique(int[] nums) {len=nums.length;choose=new int[len];Arrays.fill(choose,1);Arrays.sort(nums);List<Integer> p=new ArrayList<>();dfs(p,0,nums);return list;}public void dfs(List<Integer> p,int num,int[] arr){if(num==len){if(!list.contains(p)){list.add(new ArrayList(p));return;}}for(int j=0;j<len;j++){if(choose[j]!=-1){choose[j]=-1;p.add(arr[j]);choose[j]=-1;dfs(p,num+1,arr);p.remove(p.size()-1);choose[j]=1;}}}
}作者:菜卷
链接:https://leetcode.cn/problems/permutations-ii/solutions/2899318/quan-pai-lie-ii-by-ashi-jian-chong-dan-l-cgpf/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
关键字:day-45 全排列 II

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: