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
摘要:題目要求判斷字符串中通過刪減單詞含有幾個字符串。例如中含有個字符串,通過分別刪除第,,個。也就是說,我們需要通過一個數據結構來記錄臨時結果從而支持我們在已知前面幾個情況的場景下對后續情況進行計算。 題目要求 Given a string S and a string T, count the number of distinct subsequences of S which equa...
摘要:用動規方法做建立長度為和的二維數組,表示的第到位子串包含不同的的第到位子串的個數。初始化當的子串長度為時,當的子串長度為時,當和子串都為時,包含,故。 Problem Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a strin...
摘要:計算元素值時,當末尾字母一樣,實際上是左方數字加左上方數字,當不一樣時,就是左方的數字。示意圖代碼如果這個字符串有個怎么辦用暴力法,對每一位開始向后檢查是否是。 Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of T in S. A su...
摘要:題目要求思路和代碼這里采用廣度優先算法加上緩存的方式來實現。我們可以看到,以一個節點作為開始構成的最長路徑長度是確定的。因此我們可以充分利用之前得到的結論來減少重復遍歷的次數。 題目要求 Given an integer matrix, find the length of the longest increasing path. From each cell, you can ei...
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...
閱讀 3077·2023-04-26 00:53
閱讀 3522·2021-11-19 09:58
閱讀 1693·2021-09-29 09:35
閱讀 3279·2021-09-28 09:46
閱讀 3852·2021-09-22 15:38
閱讀 2692·2019-08-30 15:55
閱讀 3006·2019-08-23 14:10
閱讀 3822·2019-08-22 18:17