国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

[LeetCode] 491. Increasing Subsequences

wupengyu / 1791人閱讀

Problem

Given an integer array, your task is to find all the different possible increasing subsequences of the given array, and the length of an increasing subsequence should be at least 2 .

Example:

Input: [4, 6, 7, 7]
Output: [[4, 6], [4, 7], [4, 6, 7], [4, 6, 7, 7], [6, 7], [6, 7, 7], [7,7], [4,7,7]]
Note:
The length of the given array will not exceed 15.
The range of integer in the given array is [-100,100].
The given array may contain duplicates, and two equal integers should also be considered as a special case of increasing sequence.
Solution
class Solution {
    public List> findSubsequences(int[] nums) {
        List> res = new ArrayList<>();
        if (nums == null || nums.length == 0) return res;
        dfs(nums, 0, new ArrayList(), res);
        return res;
    }
    private void dfs(int[] nums, int start, List temp, List> res) {
        if (temp.size() > 1) {
            res.add(new ArrayList<>(temp));
        }
        Set used = new HashSet<>();
        for (int i = start; i < nums.length; i++) {
            if (used.contains(nums[i])) continue;
            if (temp.size() == 0 || nums[i] >= temp.get(temp.size()-1)) {
                temp.add(nums[i]);
                used.add(nums[i]);
                dfs(nums, i+1, temp, res); //next dfs doesn"t have used, yeah
                temp.remove(temp.size()-1);
            }
        }
    }
}

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/72406.html

相關文章

  • leetcode115. Distinct Subsequences

    摘要:題目要求判斷字符串中通過刪減單詞含有幾個字符串。例如中含有個字符串,通過分別刪除第,,個。也就是說,我們需要通過一個數據結構來記錄臨時結果從而支持我們在已知前面幾個情況的場景下對后續情況進行計算。 題目要求 Given a string S and a string T, count the number of distinct subsequences of S which equa...

    NSFish 評論0 收藏0
  • [LintCode/LeetCode] Distinct Subsequences [一維DP]

    摘要:用動規方法做建立長度為和的二維數組,表示的第到位子串包含不同的的第到位子串的個數。初始化當的子串長度為時,當的子串長度為時,當和子串都為時,包含,故。 Problem Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a strin...

    dailybird 評論0 收藏0
  • [Leetcode] Distinct Subsequences 不同順序字串

    摘要:計算元素值時,當末尾字母一樣,實際上是左方數字加左上方數字,當不一樣時,就是左方的數字。示意圖代碼如果這個字符串有個怎么辦用暴力法,對每一位開始向后檢查是否是。 Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of T in S. A su...

    SnaiLiu 評論0 收藏0
  • leetcode 329. Longest Increasing Path in a Matrix

    摘要:題目要求思路和代碼這里采用廣度優先算法加上緩存的方式來實現。我們可以看到,以一個節點作為開始構成的最長路徑長度是確定的。因此我們可以充分利用之前得到的結論來減少重復遍歷的次數。 題目要求 Given an integer matrix, find the length of the longest increasing path. From each cell, you can ei...

    heartFollower 評論0 收藏0
  • [LeetCode] 329. Longest Increasing Path in a Matri

    Problem Given an integer matrix, find the length of the longest increasing path. From each cell, you can either move to four directions: left, right, up or down. You may NOT move diagonally or move ou...

    hss01248 評論0 收藏0

發表評論

0條評論

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