成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[LintCode/LeetCode] Subsets & Subsets II

tracy / 1342人閱讀

Subsets Problem

Given a set of distinct integers, return all possible subsets.

Notice

Elements in a subset must be in non-descending order.
The solution set must not contain duplicate subsets.

Example

If S = [1,2,3], a solution is:

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

Can you do it in both recursively and iteratively?

Solution
class Solution {
    public ArrayList> subsets(int[] nums) {
        ArrayList> res = new ArrayList<>();
        if (nums == null || nums.length == 0) return res;
        ArrayList cur = new ArrayList<>();
        Arrays.sort(nums);
        dfs(nums, cur, res, 0);
        return res;
    }
    public void dfs(int[] nums, ArrayList cur, ArrayList> res, int index) {
        if (index > nums.length) return;
        res.add(new ArrayList (cur));
        for (int i = index; i < nums.length; i++) {
            cur.add(nums[i]);
            dfs(nums, cur, res, i+1);
            cur.remove(cur.size()-1);
        }
    }
}
Subsets II Problem

Given a list of numbers that may has duplicate numbers, return all possible subsets

##Notice

Each element in a subset must be in non-descending order.
The ordering between two subsets is free.
The solution set must not contain duplicate subsets.
Have you met this question in a real interview? Yes

Example

If S = [1,2,2], a solution is:

[
  [2],
  [1],
  [1,2,2],
  [2,2],
  [1,2],
  []
]
Challenge

Can you do it in both recursively and iteratively?

Solution
class Solution {
    public ArrayList> subsetsWithDup(ArrayList S) {
        ArrayList> res = new ArrayList<>();
        Collections.sort(S);
        ArrayList cur = new ArrayList<>();
        dfs(S, cur, res, 0);
        return res;
    }
    public void dfs(ArrayList S, ArrayList cur, ArrayList> res, int index) {
        if (index > S.size()) return;
        res.add(new ArrayList (cur));
        for (int i = index; i < S.size(); i++) {
            if (i != index && S.get(i) == S.get(i-1)) continue;
            cur.add(S.get(i));
            dfs(S, cur, res, i+1);
            cur.remove(cur.size()-1);
        }
    }
}

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

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/65006.html

相關(guān)文章

  • 【LC總結(jié)】回溯 (Subsets I II/Permutation I II/Combinatio

    摘要:不同數(shù)包含重復(fù)數(shù)為的時(shí)候,表示在外層的循環(huán)正在被使用,所以當(dāng)前循環(huán)遇到為一定要跳過(guò)。對(duì)當(dāng)前循環(huán)要添加的數(shù)組,在添加當(dāng)前元素后進(jìn)行遞歸,遞歸之后要將當(dāng)前元素的使用標(biāo)記改為,表示已經(jīng)使用和遞歸完畢,然后再將這個(gè)元素從的末位刪除。 Subsets Problem Given a set of distinct integers, nums, return all possible subse...

    tuomao 評(píng)論0 收藏0
  • leetcode90. Subsets II

    摘要:題目要求可以先參考關(guān)于的這篇博客再看接下來(lái)的內(nèi)容。思路一鏈表的形式我們可以通過(guò)例子,,,來(lái)說(shuō)明。在遞歸中我們會(huì)將起始下標(biāo)后的值依次加入當(dāng)前結(jié)果集,并將結(jié)果集加入結(jié)果集數(shù)組中。如果遇到重復(fù)的數(shù)字,則繼續(xù)遍歷下一個(gè)數(shù)字,直至遍歷結(jié)束。 題目要求 Given a collection of integers that might contain duplicates, nums, retur...

    PascalXie 評(píng)論0 收藏0
  • leetcode-90. Subsets II

    摘要:題目描述注意題目解讀找出所有的子集。思路確定子集的來(lái)源,遍歷原始列表,每一個(gè)元素都往已有的子集列表里邊添加,同時(shí)添加到已有的子集中去,產(chǎn)生新的子集。類(lèi)似于動(dòng)態(tài)規(guī)劃思想,依賴于之前的東西產(chǎn)生現(xiàn)在的東西。 題目描述 Given a collection of integers that might contain duplicates, nums, return all possible ...

    lijinke666 評(píng)論0 收藏0
  • Subsets 系列 Leetcode解題記錄

    摘要:寫(xiě)這個(gè)系列是因?yàn)榧o(jì)念一下去年的今天,就是年的月號(hào),刷題第一天,今天是一周年紀(jì)念日。排除,就是返回一空的。復(fù)雜度分析算法課講過(guò),這個(gè)復(fù)雜度是指數(shù)次,能實(shí)現(xiàn)出來(lái)就行了,沒(méi)法優(yōu)化。復(fù)雜度分析不分析了,反正指數(shù)次。 Subsets 寫(xiě)這個(gè)系列是因?yàn)榧o(jì)念一下去年的今天,就是2015年的9月14號(hào),刷題第一天,今天是一周年紀(jì)念日。當(dāng)時(shí)只敢做easy還得抄答案的我想啥時(shí)候能做上medium啊,事到如...

    gityuan 評(píng)論0 收藏0
  • [Leetcode] Subset 子集

    摘要:深度優(yōu)先搜索復(fù)雜度時(shí)間空間遞歸棧空間思路這道題可以轉(zhuǎn)化為一個(gè)類(lèi)似二叉樹(shù)的深度優(yōu)先搜索。另外需要先排序以滿足題目要求。新的集合要一個(gè)新的,防止修改引用。 Subset I Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must be in n...

    hzc 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<