亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

[LeetCode] Permutations I / II

shery / 1830人閱讀

Permutations I Problem

Given a list of numbers, return all possible permutations.

Example

For nums = [1,2,3], the permutations are:

[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]

Challenge

Do it without recursion.

Solution

Recursion

class Solution {
    public ArrayList> permute(ArrayList num) {
        ArrayList> res = new ArrayList>();
        if (num == null || num.size() == 0) {
            return res;
        }
        ArrayList list = new ArrayList ();
        helper (nuts, list, res);
        return res;
    }
    
    public void helper(ArrayList num, ArrayList list, ArrayList> res) {
        if (list.size() == num.size()) {
            res.add(new ArrayList (list));
            return;
        }
        for (int i = 0; i < num.size(); i++) {
            if (list.contains(num.get(i))) {
                continue;
            }
            list.add(num.get(i));
            helper(num, list, res);
            list.remove(list.size() - 1);
        }
    }
}

Permutations II Problem

Given a collection of numbers that might contain duplicates, return all possible unique permutations.

Example:

Input: [1,1,2]
Output:
[
  [1,1,2],
  [1,2,1],
  [2,1,1]
]
Solution

sort

call backtracking function in main function

in backtracking function, when temp list size equals nums array size, save a copy of temp list to result

iteration nums array, if current num is used, or it"s same as previous num and previous num is unused (released), continue

update temp array and used boolean array at the same time in back tracking.

class Solution {
    public List> permuteUnique(int[] nums) {
        List> res = new ArrayList<>();
        if (nums == null || nums.length == 0) return res;
        Arrays.sort(nums);
        helper(nums, new ArrayList(), new boolean[nums.length], res);
        return res;
    }
    private void helper(int[] nums, List temp, boolean[] used, List> res) {
        if (temp.size() == nums.length) res.add(new ArrayList<>(temp));
        else {
            for (int i = 0; i < nums.length; i++) {
                if (used[i] || (i > 0 && !used[i-1] && nums[i] == nums[i-1])) {
                    continue;
                } else {
                    used[i] = true;
                    temp.add(nums[i]);
                    helper(nums, temp, used, res);
                    temp.remove(temp.size()-1);
                    used[i] = false;
                }
            }
        }
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/65480.html

相關(guān)文章

  • leetcode47 Permutations II

    摘要:當(dāng)前的值如果已經(jīng)被使用過,則繼續(xù)判斷下一個數(shù)值。則當(dāng)?shù)谝粋€被添加進(jìn)結(jié)果集時,可以繼續(xù)使用第二個作為元素添加進(jìn)結(jié)果集從而生成。假設(shè)將表示為那么結(jié)果集中會確保永遠(yuǎn)在數(shù)值的前面,從而避免了和的重復(fù)情況出現(xiàn)。 題目要求 Given a collection of numbers that might contain duplicates, return all possible unique ...

    taoszu 評論0 收藏0
  • leetcode 47 Permutations II

    摘要:題目詳情題目要求輸入一個可能會有重復(fù)數(shù)字的數(shù)組,要求我們輸出可能組成的全排列無重復(fù)排列??梢杂脕韺崿F(xiàn),但這種實現(xiàn)方式復(fù)雜度高。另外一種實現(xiàn)思路是,新聲明一個數(shù)組來存儲中元素的使用狀況。以這個數(shù)組為例。 題目詳情 Given a collection of numbers that might contain duplicates, return all possible unique ...

    Cobub 評論0 收藏0
  • [Leetcode] Permutations 全排列

    摘要:每一輪搜索選擇一個數(shù)加入列表中,同時我們還要維護(hù)一個全局的布爾數(shù)組,來標(biāo)記哪些元素已經(jīng)被加入列表了,這樣在下一輪搜索中要跳過這些元素。 Permutations I Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the following permu...

    scq000 評論0 收藏0
  • [Leetcode]PermutationsI II Next Permutation Permut

    摘要:解題思路這道題是要將排列按字典序排列,然后求出下一個排列,一種辦法是我們先求出所有的排序情況,但是題目規(guī)定不能占有額外空間。每次求出一個數(shù)字后,要及時的把它從中刪除掉。采用來構(gòu)造結(jié)果序列。 PermutationsGiven a collection of distinct numbers, return all possible permutations. For example, ...

    ChristmasBoy 評論0 收藏0
  • leetcode 部分解答索引(持續(xù)更新~)

    摘要:前言從開始寫相關(guān)的博客到現(xiàn)在也蠻多篇了。而且當(dāng)時也沒有按順序?qū)懍F(xiàn)在翻起來覺得蠻亂的??赡艽蠹铱粗卜浅2环奖?。所以在這里做個索引嘻嘻。順序整理更新更新更新更新更新更新更新更新更新更新更新更新更新更新更新更新 前言 從開始寫leetcode相關(guān)的博客到現(xiàn)在也蠻多篇了。而且當(dāng)時也沒有按順序?qū)憽F(xiàn)在翻起來覺得蠻亂的??赡艽蠹铱粗卜浅2环奖恪K栽谶@里做個索引嘻嘻。 順序整理 1~50 1...

    leo108 評論0 收藏0

發(fā)表評論

0條評論

shery

|高級講師

TA的文章

閱讀更多
最新活動
閱讀需要支付1元查看
<