day25 回溯算法-必做题(递增子序列+全排列*2)+选做题没做下次一定(重新安排行程+N皇后+解数独)

### 7.11 491. 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.
https://leetcode.cn/problems/non-decreasing-subsequences/description/

https://programmercarl.com/0491.%E9%80%92%E5%A2%9E%E5%AD%90%E5%BA%8F%E5%88%97.html 
不取的元素:比path最后一个元素小的
```java
public class nonDecSub {  
    List<List<Integer>> result = new LinkedList<>();  
    LinkedList<Integer> path = new LinkedList<>();  
  
    public List<List<Integer>> findSubsequences(int[] nums) {  
        backtracking(nums,0);  
        return result;  
    }  
    private void backtracking(int[] nums, int startIndex){  
        if(path.size() > 1){  
            result.add(new ArrayList<>(path));  
        }  
        Set<Integer> used = new HashSet<>();  
        for (int i = startIndex; i < nums.length; i++) {  
            //不取的元素:比path最后一个元素小的,同一级递归中出现过的  
            if(path.size() != 0 && nums[i] < path.get(path.size()-1) || used.contains(nums[i])){  
                continue;  
            }  
            used.add(nums[i]);  
            path.add(nums[i]);  
            backtracking(nums,i+1);  
            path.removeLast();  
        }  
    }  
}  
class nonDecSubTest {  
    public static void main(String[] args) {  
        nonDecSub example = new nonDecSub();  
        int[] nums = {4,6,8,8,7};  
        List<List<Integer>> result = example.findSubsequences(nums);  
        for(List<Integer> levels :result){  
            System.out.print("[");  
            for(int i : levels){  
                System.out.print(i+" ");  
            }  
            System.out.println("]");  
        }  
    }  
}
```
### 7.12 46. Permutations
Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order.
https://leetcode.cn/problems/permutations/
https://programmercarl.com/0046.%E5%85%A8%E6%8E%92%E5%88%97.html   

```java
public class permutations46 {  
    List<List<Integer>> result = new LinkedList<>();  
    LinkedList<Integer> path = new LinkedList<>();  
  
    public List<List<Integer>> permute(int[] nums) {  
        int[] used = new int[nums.length];  
        backtracking(nums,used);  
        return result;  
    }  
    private void backtracking(int[] nums, int[] used){  
        if(path.size() == nums.length){  
            result.add(new ArrayList<>(path));  
            return;  
        }  
        for (int i = 0; i < nums.length; i++) {  
            if(used[i] == 1) continue;  
            path.add(nums[i]);  
            used[i] = 1;  
            backtracking(nums,used);  
            path.removeLast();  
            used[i] = 0;  
        }  
    }  
}  
class permutations46Test {  
    public static void main(String[] args) {  
        permutations46 example = new permutations46();  
        int[] nums = {4,6,8};  
        List<List<Integer>> result = example.permute(nums);  
        for(List<Integer> levels :result){  
            System.out.print("[");  
            for(int i : levels){  
                System.out.print(i+" ");  
            }  
            System.out.println("]");  
        }  
    }  
}
```
### 7.13 47. Permutations II
Given a collection of numbers, nums, that might contain duplicates, return all possible unique permutations in any order.
https://leetcode.cn/problems/permutations-ii/description/
47.全排列 II 
本题 就是我们讲过的 40.组合总和II 去重逻辑 和 46.全排列 的结合,可以先自己做一下,然后重点看一下 文章中 我讲的拓展内容: used[i - 1] == true 也行,used[i - 1] == false 也行 
https://programmercarl.com/0047.%E5%85%A8%E6%8E%92%E5%88%97II.html     
先自己写了一下,试出来了usedSet.contains(nums[i]后continue,但不知道为什么。明天看讲解吧。
```java
public class permutations47 {  
    List<List<Integer>> result = new LinkedList<>();  
    LinkedList<Integer> path = new LinkedList<>();  
  
    public List<List<Integer>> permuteUnique(int[] nums) {  
        int[] used = new int[nums.length];  
        backtracking(nums,used);  
        return result;  
    }  
    private void backtracking(int[] nums, int[] used){  
        if(path.size() == nums.length){  
            result.add(new ArrayList<>(path));  
            return;  
        }  
        Set<Integer> usedSet = new HashSet<>();  
        for (int i = 0; i < nums.length; i++) {  
            if(used[i] != 0 || usedSet.contains(nums[i])) continue;  
  
            path.add(nums[i]);  
            used[i] = 1;  
            usedSet.add(nums[i]);  
  
            backtracking(nums,used);  
  
            path.removeLast();  
            used[i] = 0;  
        }  
    }  
}  
class permutations47Test {  
    public static void main(String[] args) {  
        permutations47 example = new permutations47();  
        int[] nums = {4,4,1,4,4};  
        List<List<Integer>> result = example.permuteUnique(nums);  
        for(List<Integer> levels :result){  
            System.out.print("[");  
            for(int i : levels){  
                System.out.print(i+" ");  
            }  
            System.out.println("]");  
        }  
    }  
}
```
下面这三道题都非常难,建议大家一刷的时候 可以适当选择跳过。 

因为 一刷 也不求大家能把这么难的问题解决,大家目前能了解一下题目的要求,了解一下解题思路,不求能直接写出代码,先大概熟悉一下这些题,二刷的时候,随着对回溯算法的深入理解,再去解决如下三题。 


### 7.14 332. Reconstruct Itinerary
You are given a list of airline tickets where tickets`[i] = [fromi, toi] represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.

All of the tickets belong to a man who departs from "JFK", thus, the itinerary must begin with "JFK". If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string.

For example, the itinerary `["JFK", "LGA"] has a smaller lexical order than ["JFK", "LGB"].
You may assume all tickets form at least one valid itinerary. You must use all the tickets once and only once.
https://leetcode.cn/problems/reconstruct-itinerary/description/
332.重新安排行程(可跳过) 
本题很难,一刷的录友刷起来 比较费力,可以留给二刷的时候再去解决。
本题没有录制视频,当初录视频是按照 《代码随想录》出版的目录来的,当时没有这道题所以就没有录制。
https://programmercarl.com/0332.%E9%87%8D%E6%96%B0%E5%AE%89%E6%8E%92%E8%A1%8C%E7%A8%8B.html  

### 7.15 51. N-Queens
The n-queens puzzle is the problem of placing n queens on an n x n chessboard such that no two queens attack each other.
Given an integer n, return all distinct solutions to the n-queens puzzle. You may return the answer in any order.
Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both indicate a queen and an empty space, respectively.
https://leetcode.cn/problems/n-queens/description/
51.N皇后(适当跳过) 
N皇后这道题目还是很经典的,一刷的录友们建议看看视频了解了解大体思路 就可以 (如果没时间本次就直接跳过) ,先有个印象,二刷的时候重点解决。  
https://programmercarl.com/0051.N%E7%9A%87%E5%90%8E.html   

### 7.16 37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells.
A sudoku solution must satisfy all of the following rules:
Each of the digits 1-9 must occur exactly once in each row.
Each of the digits 1-9 must occur exactly once in each column.
Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid.
The '.' character indicates empty cells.
https://leetcode.cn/problems/sudoku-solver/description/

37.解数独(适当跳过)  
同样,一刷的录友们建议看看视频了解了解大体思路(如果没时间本次就直接跳过),先有个印象,二刷的时候重点解决。 
https://programmercarl.com/0037.%E8%A7%A3%E6%95%B0%E7%8B%AC.html   
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值