Skip to content

Latest commit

 

History

History
28 lines (25 loc) · 721 Bytes

2023-01-20.md

File metadata and controls

28 lines (25 loc) · 721 Bytes

题目

  1. Non-decreasing Subsequences

Given an integer array nums, return all the different possible non-decreasing subsequences of the given array with at least two elements. You may return the answer in any order.

思路

代码

var findSubsequences = function(nums) {
  const res = []
  function dfs(i, subseq) {
    const last = subseq[subseq.length-1]
    const visited = []
    for (let j = i; j < nums.length; j++) {
      if (visited[nums[j]] || nums[j] < last) continue
      visited[nums[j]] = 1
      const newSubseq = subseq.concat(nums[j])
      if (newSubseq.length > 1) {
        res.push(newSubseq)
      }
      dfs(j+1, newSubseq)
    }
  }
  dfs(0, [])
  return res
}