LeetCode刷题 Day29

60 阅读1分钟

LeetCode刷题 Day29

491. Increasing Subsequences

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

The given array may contain duplicates, and two equal integers should also be considered a special case of increasing sequence.

 

Example 1:

Input: nums = [4,6,7,7]
Output: [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]

Example 2:

Input: nums = [4,4,3,2,1]
Output: [[4,4]]

思路:

  • 这道题的重点依然是去重,比如example1, 比如如何避免[6,7]的重复出现。 首先考虑纵向去重,这样就可以避免同一个index的数字在纵向重复使用,但是这样不能避免[6,7]重复出现 所以这里要引入used cache去记录本层的数字是否曾经被使用过

代码:

var findSubsequences = function(nums) {
    let result = [];
    let path = [];
    
    var helper = function(path, startIndex) {
        if (path.length > 1) {
            result.push([...path]);            
        }
        let used = {}; // reset the used cache
        for (let i = startIndex; i < nums.length; i++) {
            if (used[nums[i]] || path[path.length - 1] > nums[i]) continue;
            used[nums[i]] = true;
            path.push(nums[i]);
            helper(path, i + 1);
            path.pop();
        }
    }
    
    helper(path, 0);
    return result;
};

46. Permutations

Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order.

Example 1:

Input: nums = [1,2,3]
Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

Example 2:

Input: nums = [0,1]
Output: [[0,1],[1,0]]

Example 3:

Input: nums = [1]
Output: [[1]]

思路:

  • 使用used cache去记录纵向路径中值是否被使用过。

代码:

var permute = function(nums) {
    let res = [];
    let helper = function(path, used) {
        if (nums.length === path.length) {
            res.push([...path]);
            return;
        }
        for (let i = 0; i < nums.length; i++) {
            if (used[nums[i]]) continue;
            used[nums[i]] = true;
            path.push(nums[i]);
            helper(path, used);
            path.pop();
            used[nums[i]] = false;
        }
    }

    helper([], {});
    return res;
};

47. Permutations II

Example 1:

Input: nums = [1,1,2]
Output:
[[1,1,2],
 [1,2,1],
 [2,1,1]]

Example 2:

Input: nums = [1,2,3]
Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

思路:

  • 排序

  • 使用used cache去做层去重,关键在于去重的条件。

    if (nums[i - 1] === nums[i] && !used[i - 1]) continue;

  • 而要让纵向递归走下去 而不是停留在同一个index上,就应该判断used[i] 是否被使用。

代码:


var permuteUnique = function(nums) {
    let result = [];
    let path = [];
    let used = {};
    
    nums.sort((a, b) => a - b);
    
    var helper = function(path) {
        if (path.length  === nums.length) {
            result.push([...path]);
            return;
        }
        
        
        for (let i = 0; i < nums.length; i++) {
            if (nums[i - 1] === nums[i] && !used[i - 1]) continue;
            
            if (!used[i]) {
                used[i] = true;          
                path.push(nums[i]);
                helper(path);
                used[i] = false;
                path.pop();
            }
        }
    }
    
    helper(path);
    return result;
};