LeetCode关于数独Sudoku的问题

关于我的 Leetcode 题目解答,代码前往 Github:https://github.com/chenxiangcyr/leetcode-answers


LeetCode题目:36. Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.
The Sudoku board could be partially filled, where empty cells are filled with the character '.'.

Valid Sudoku

A partially filled sudoku which is valid.

Note:

  • A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
class Solution {
    public boolean isValidSudoku(char[][] board) {
        int n = board.length;
        
        for(int i = 0; i < n; i++) {
            Set<Character> rows = new HashSet<Character>();
            Set<Character> columns = new HashSet<Character>();
            Set<Character> cubes = new HashSet<Character>();
            
            for(int j = 0; j < n; j++) {
                if(board[i][j] != '.') {
                    // check each row
                    if(rows.contains(board[i][j])) {
                        return false;
                    }
                    else {
                        rows.add(board[i][j]);
                    }
                }
                
                if(board[j][i] != '.') {
                    // check each column
                    if(columns.contains(board[j][i])) {
                        return false;
                    }
                    else {
                        columns.add(board[j][i]);
                    }
                }
                
                int rowIdx = 3 * (i / 3);
                int columnIdx = 3 * (i % 3);
                if(board[rowIdx + j / 3][columnIdx + j % 3] != '.') {
                    // check each cube
                    if(cubes.contains(board[rowIdx + j / 3][columnIdx + j % 3])) {
                        return false;
                    }
                    else {
                        cubes.add(board[rowIdx + j / 3][columnIdx + j % 3]);
                    }
                }
            }
        }
        
        return true;
    }
}

LeetCode题目:37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells.
Empty cells are indicated by the character '.'.
You may assume that there will be only one unique solution.

Sudoku Solver

Sudoku Solver
class Solution {
    public void solveSudoku(char[][] board) {
        if(board == null || board.length == 0 || board[0].length == 0) {
            return;
        }
        
        solve(board);
    }
    
    public boolean solve(char[][] board){
        for(int i = 0; i < board.length; i++) {
            for(int j = 0; j < board[0].length; j++) {
                if(board[i][j] == '.') {
                    // 尝试每一个数字
                    for(char c = '1'; c <= '9'; c++) {
                        if(isValid(board, i, j, c)){
                            board[i][j] = c;
                            
                            // 递归,处理下一个位置
                            if(solve(board)) {
                                return true;
                            }
                            else {
                                // 回溯 backtracking
                                board[i][j] = '.';
                            }
                        }
                    }
                    
                    return false;
                }
            }
        }
        
        return true;
    }
    
    // 把(row, col)位置设为c,判断是不是合法的数独
    private boolean isValid(char[][] board, int row, int col, char c){
        for(int i = 0; i < 9; i++) {
            // 检查第col列,不能有重复的c
            if(board[i][col] != '.' && board[i][col] == c) {
                return false;
            }
            
            // 检查第row行,不能有重复的c
            if(board[row][i] != '.' && board[row][i] == c) {
                return false;
            }
            
            // 检查对应的cube,不能有重复的c
            if(board[3 * (row / 3) + i / 3][ 3 * (col / 3) + i % 3] != '.' && 
board[3 * (row / 3) + i / 3][3 * (col / 3) + i % 3] == c) {
                return false;
            }
        }
        
        return true;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,157评论 0 10
  • The Inner Game of Tennis W Timothy Gallwey Jonathan Cape ...
    网事_79a3阅读 14,258评论 3 20
  • 好啦,好啦 给你安稳的吻 你感觉潮湿吗 那是因为我爱你啊 所以别擦去,带着它吧 当我变成鹿的时候才能认出你。
    梵夜阅读 1,397评论 0 0
  • 简书里面有展台 五湖四海聚英才 香飘翰墨流芳韵 书法写字笔下来 自勉 幽雅闲情我自知 春节将近写佳词 诗词格律填新...
    超生妈妈阅读 1,020评论 0 7
  • 走在这样的路上乐此不疲 嘿,哈,嘿,哈 这样的声音 络绎不绝 我曾经羡慕他们 也想和他们一样 站在没有水的南湖旁边...
    小幽余生不加糖阅读 1,033评论 0 0