Skip to content

491.递增子序列

题解

js
/**
 * @param {number[]} nums
 * @return {number[][]}
 */
var findSubsequences = function (nums) {
  const res = [];
  function fn(startIndex, path) {
    path.length >= 2 && res.push([...path]);
    const used = {};
    for (let i = startIndex; i < nums.length; i++) {
      if ((path.length > 0 && path[path.length - 1] > nums[i]) || used[nums[i]])
        continue;
      used[nums[i]] = true;
      fn(i + 1, path.concat(nums[i]));
    }
  }
  fn(0, []);
  return res;
};
/**
 * @param {number[]} nums
 * @return {number[][]}
 */
var findSubsequences = function (nums) {
  const res = [];
  function fn(startIndex, path) {
    path.length >= 2 && res.push([...path]);
    const used = {};
    for (let i = startIndex; i < nums.length; i++) {
      if ((path.length > 0 && path[path.length - 1] > nums[i]) || used[nums[i]])
        continue;
      used[nums[i]] = true;
      fn(i + 1, path.concat(nums[i]));
    }
  }
  fn(0, []);
  return res;
};