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); } } } }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://m.specialneedsforspecialkids.com/yun/72406.html
摘要:題目要求判斷字符串中通過刪減單詞含有幾個(gè)字符串。例如中含有個(gè)字符串,通過分別刪除第,,個(gè)。也就是說,我們需要通過一個(gè)數(shù)據(jù)結(jié)構(gòu)來記錄臨時(shí)結(jié)果從而支持我們?cè)谝阎懊鎺讉€(gè)情況的場(chǎng)景下對(duì)后續(xù)情況進(jìn)行計(jì)算。 題目要求 Given a string S and a string T, count the number of distinct subsequences of S which equa...
摘要:用動(dòng)規(guī)方法做建立長度為和的二維數(shù)組,表示的第到位子串包含不同的的第到位子串的個(gè)數(shù)。初始化當(dāng)?shù)淖哟L度為時(shí),當(dāng)?shù)淖哟L度為時(shí),當(dāng)和子串都為時(shí),包含,故。 Problem Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a strin...
摘要:計(jì)算元素值時(shí),當(dāng)末尾字母一樣,實(shí)際上是左方數(shù)字加左上方數(shù)字,當(dāng)不一樣時(shí),就是左方的數(shù)字。示意圖代碼如果這個(gè)字符串有個(gè)怎么辦用暴力法,對(duì)每一位開始向后檢查是否是。 Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of T in S. A su...
摘要:題目要求思路和代碼這里采用廣度優(yōu)先算法加上緩存的方式來實(shí)現(xiàn)。我們可以看到,以一個(gè)節(jié)點(diǎn)作為開始構(gòu)成的最長路徑長度是確定的。因此我們可以充分利用之前得到的結(jié)論來減少重復(fù)遍歷的次數(shù)。 題目要求 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...
閱讀 3086·2023-04-26 00:53
閱讀 3536·2021-11-19 09:58
閱讀 1700·2021-09-29 09:35
閱讀 3290·2021-09-28 09:46
閱讀 3869·2021-09-22 15:38
閱讀 2698·2019-08-30 15:55
閱讀 3016·2019-08-23 14:10
閱讀 3831·2019-08-22 18:17