代码随想录day 28 491. 递增子序列 | * 46.全排列 | 47.全排列 II

发布时间 2023-03-29 20:49:49作者: 刷刷题啊呀呀

给定一个整型数组, 你的任务是找到所有该数组的递增子序列,递增子序列的长度至少是2。

示例:

  • 输入: [4, 6, 7, 7]
  • 输出: [[4, 6], [4, 7], [4, 6, 7], [4, 6, 7, 7], [6, 7], [6, 7, 7], [7,7], [4,7,7]]

说明:

  • 给定数组的长度不会超过15。
  • 数组中的整数范围是 [-100,100]。
  • 给定数组中可能包含重复数字,相等的数字应该被视为递增的一种情况

    class Solution {
        public List<List<Integer>> findSubsequences(int[] nums) {
            List<List<Integer>> res = new ArrayList<>();
            helper(nums, res, new ArrayList<>(), 0);
            return res;
        }
    
        private void helper(int[] nums, List<List<Integer>> res, List<Integer> cur, int idx) {
            if (cur.size() > 1) res.add(new ArrayList<>(cur));
            int[] used = new int[201];
            for (int i = idx; i < nums.length; i++) {
                 if (!cur.isEmpty() && nums[i] < cur.get(cur.size() - 1) ||
                        (used[nums[i] + 100] == 1)) continue;
                 used[nums[i] + 100] = 1;
                 cur.add(nums[i]);  
                 helper(nums, res, cur, i + 1);
                 cur.remove(cur.size() - 1);
            }
        }
    }

    46. 全排 

   给定一个不含重复数字的数组 nums ,返回其 所有可能的全排列 。你可以 按任意顺序 返回答案。
  输入:nums = [1,2,3]

  输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

class Solution {
    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        helper(result, new ArrayList<>(),nums);
        return result;
    }
    private void helper(List<List<Integer>> result, List<Integer> path, int[] nums) {
        if (path.size() == nums.length) {
            result.add(new ArrayList<>(path));
        }
        for (int i = 0;i  < nums.length; i++) {
            if (path.contains(nums[i])) {
                continue;
            }
            path.add(nums[i]);
            helper(result, path, nums);
            path.remove(path.size() - 1);
        }
    }
}

47.全排列 II

给定一个可包含重复数字的序列 nums ,按任意顺序 返回所有不重复的全排列。

示例 1:

  • 输入:nums = [1,1,2]
  • 输出: [[1,1,2], [1,2,1], [2,1,1]]

示例 2:

  • 输入:nums = [1,2,3]
  • 输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

提示:

  • 1 <= nums.length <= 8
  • -10 <= nums[i] <= 10

class Solution {
    public List<List<Integer>> permuteUnique(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        Arrays.sort(nums);
        boolean[] used = new boolean[nums.length];
        Arrays.fill(used, false);
        helper(nums, used, result, new ArrayList<>());
        return result;
    }

    private void helper(int[] nums, boolean[] used, List<List<Integer>> result, List<Integer> path) {
        if (path.size() == nums.length) {
            result.add(new ArrayList<>(path));
            return;
        }

        for (int i = 0; i < nums.length; i++) {
            if (i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false) {
                continue;
            }
            if (used[i] == false) {
                used[i] = true;
                path.add(nums[i]);
                helper(nums, used, result, path);
                path.remove(path.size() - 1);
                used[i] = false;
            }
        }
    }
}