• 代码随想录二刷day30


    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档



    一、力扣332. 重新安排行程

    class Solution {
        private LinkedList<String> res;
        private LinkedList<String> path = new LinkedList<>();
    
        public List<String> findItinerary(List<List<String>> tickets) {
            Collections.sort(tickets, (a, b) -> a.get(1).compareTo(b.get(1)));
            path.add("JFK");
            boolean[] used = new boolean[tickets.size()];
            backTracking((ArrayList) tickets, used);
            return res;
        }
    
        public boolean backTracking(ArrayList<List<String>> tickets, boolean[] used) {
            if (path.size() == tickets.size() + 1) {
                res = new LinkedList(path);
                return true;
            }
    
            for (int i = 0; i < tickets.size(); i++) {
                if (!used[i] && tickets.get(i).get(0).equals(path.getLast())) {
                    path.add(tickets.get(i).get(1));
                    used[i] = true;
    
                    if (backTracking(tickets, used)) {
                        return true;
                    }
    
                    used[i] = false;
                    path.removeLast();
                }
            }
            return false;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    二、力扣51. N 皇后

    class Solution {
        List<List<String>> res = new ArrayList<>();
        public List<List<String>> solveNQueens(int n) {
            char[][] ch = new char[n][n];
            for(char[] c:ch){
                Arrays.fill(c, '.');
            }
            fun(0, n, ch);
            return res;
        }
        public void fun(int row, int n, char[][] ch){
            if(row == n){
                res.add(new ArrayList<>(temp(ch)));
                return;
            }
            for(int col = 0; col < n; col ++){
                if(flag(ch, row, col, n)){
                    ch[row][col] = 'Q';
                    fun(row +1, n, ch);
                    ch[row][col] = '.';
                }
            }
        }
        public List<String> temp(char[][] ch){
            List<String> li = new ArrayList<>();
            for(char[] c: ch){
                li.add(String.copyValueOf(c));
            }
            return li;
        }
        public boolean flag(char[][] ch, int row, int col, int n){
            //判断行
            for(int i = 0; i < col; i ++){
                if(ch[row][i] == 'Q'){
                    return false;
                }
            }
            //判断列
            for(int i = 0; i < row; i ++){
                if(ch[i][col] == 'Q'){
                    return false;
                }
            }
            //判断°角
            for(int i = row-1, j = col-1; i >= 0 && j >= 0; i --, j --){
                if(ch[i][j] == 'Q'){
                    return false;
                }
            }
            //判断135度角
            for(int i = row-1, j = col+1; i >= 0 && j <= n-1; i--, j ++){
                if(ch[i][j] == 'Q'){
                    return false;
                }
            }
            return true;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    三、力扣37. 解数独

    class Solution {
        public void solveSudoku(char[][] board) {
            fun(board);
        }
        public boolean fun(char[][] board){
            for(int i = 0; i < 9; i ++){
                for(int j = 0; j < 9; j ++){
                    if(board[i][j] != '.'){
                        continue;
                    }
                    for(char k = '1'; k <= '9'; k ++){
                        if(flag(board, i, j, k)){
                            board[i][j] = k;
                            if(fun(board)){
                                return true;
                            }
                            board[i][j] = '.';
                        }
                    }
                    return false;
                }
            }
            return true;
        }
        public boolean flag(char[][] board, int row, int col, char k){
            //判断行
            for(int i = 0; i < 9; i ++){
                if(board[row][i] == k){
                    return false;
                }
            }
            //判断列
            for(int i = 0; i < 9; i ++){
                if(board[i][col] == k){
                    return false;
                }
            }
            //判断9宫格
            int a = (row/3)*3;
            int b = (col/3)*3;
            for(int i = a; i < a + 3; i ++){
                for(int j = b; j < b + 3; j ++){
                    if(board[i][j] == k){
                        return false;
                    }
                }
            }
            return true;
        }
    }```
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
  • 相关阅读:
    戴尔股价节节攀升,但去中国化导致老本行正被中国消费者抛弃
    JavaScript高级 |如何玩转箭头函数?
    Linux--CE--ansible安装以及配置主控服务器和被控服务器
    2023NOIP A层联测25-滈葕
    基于Java+SpringBoot+Vue物流管理小程序系统的设计与实现 前后端分离【Java毕业设计·文档报告·代码讲解·安装调试】
    经济型EtherCAT运动控制器(七):运动缓冲
    【2022年11月19日提高A组】消圈策略【DP】
    .NET 7 预览版2 中的 ASP.NET Core 更新
    基于Jeecgboot前后端分离的ERP系统开发数据库设计(一)
    Spring基础:快速入门spring cloud(1):Spring Cloud介绍
  • 原文地址:https://blog.csdn.net/ResNet156/article/details/132939102