My Fifty-Ninth 全排列Ⅱ - By Nicolas
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                My Fifty-Ninth 全排列Ⅱ - By Nicolas
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.                        
                                這篇page是針對leetcode上的47.全排列Ⅱ所寫的。小尼先簡單的說明一下這道題的意思,給定一個可包含重復(fù)數(shù)字的序列nums,按照任意順序返回全排列。這道題跟小尼上一篇所寫的唯一的區(qū)別就是這里面的元素可以重復(fù),但是返回的數(shù)組都是一樣的。小尼先拉一下代碼:
class Solution {List<List<Integer>> result = new ArrayList<>();List<Integer> path = new ArrayList<>();public List<List<Integer>> permuteUnique(int[] nums) {boolean[] used = new boolean[nums.length];Arrays.fill(used, false);Arrays.sort(nums);backTrack(nums,used);return result;}public void backTrack(int[] nums , boolean[] used){if(path.size() == nums.length){result.add(new ArrayList<>(path));return;}for(int i = 0; i < nums.length ; i++){if (i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false) {continue;}if(used[i] == false){used[i] = true;path.add(nums[i]);backTrack(nums,used);path.remove(path.size() - 1);used[i] = false;}}} }這里相比較于數(shù)字不重復(fù)的那道題這里多做了一項used判斷,used判斷就是是否這數(shù)字被調(diào)用過。
總結(jié)
以上是生活随笔為你收集整理的My Fifty-Ninth 全排列Ⅱ - By Nicolas的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: Google工具栏(转)
- 下一篇: 浅谈布尔代数
