首页 > 其他分享 >40 90 47 491,给二刷的自己提醒,这四个一起做

40 90 47 491,给二刷的自己提醒,这四个一起做

时间:2023-01-13 01:22:21浏览次数:43  
标签:used nums int 47 40 二刷 new path size

491. 递增子序列

LinkedList<Integer> path = new LinkedList<>();
    List<List<Integer>> result = new ArrayList<>();

    public List<List<Integer>> findSubsequences(int[] nums) {
        backTracing(nums, 0);
        return result;
    }

    public void backTracing(int[] nums, int startIndex) {
        if (path.size() > 1) {
            result.add(new ArrayList<>(path));
        }

        boolean[] used = used = new boolean[201];
        for (int i = startIndex; i < nums.length; i++) {
            if ((!path.isEmpty() && path.getLast() > nums[i]) || used[nums[i] + 100]) {
                continue;
            }
            used[nums[i] + 100] = true;
            path.add(nums[i]);
            backTracing(nums, i + 1);
            path.removeLast();
        }
    }

46. 全排列


``List<Integer> path = new ArrayList<>();
    List<List<Integer>> res = new ArrayList<>();
    boolean[] used;

    public List<List<Integer>> permute(int[] nums) {
        used = new boolean[nums.length];
        backTracing(nums, nums.length);
        return res;
    }


    public void backTracing(int[] nums, int size) {
        if (path.size() == size) {
            res.add(new ArrayList<>(path));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (!used[i]) {
                path.add(nums[i]);
                used[i] = true;
            }else {
                continue;
            }
            backTracing(nums, size);
            path.remove(path.size()-1);
            used[i] = false;
        }
    }`

### 47. 全排列 II

```java\
List<Integer> path = new ArrayList<>();
    List<List<Integer>> res = new ArrayList<>();
    boolean[] used;
    public List<List<Integer>> permuteUnique(int[] nums) {
        Arrays.sort(nums);
        used = new boolean[nums.length];
        backTracing2(nums,nums.length);
        return res;
    }

    public void backTracing2(int[] nums, int size) {
        if (path.size() == size) {
            res.add(new ArrayList<>(path));
        }
        for (int i = 0; i < nums.length; i++) {
            if (i > 0 && nums[i] == nums[i - 1] && !used[i - 1]) {
                continue;
            }
            if (!used[i]) {
                path.add(nums[i]);
                used[i] = true;
                backTracing2(nums, size);
                used[i] = false;
                path.remove(path.size() - 1);
            }
        }
    }

标签:used,nums,int,47,40,二刷,new,path,size
From: https://www.cnblogs.com/Chain-Tian/p/17048389.html

相关文章