LeetCode關(guān)于數(shù)獨Sudoku的問題

關(guān)于我的 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] == '.') {
                    // 嘗試每一個數(shù)字
                    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)位置設(shè)為c,判斷是不是合法的數(shù)獨
    private boolean isValid(char[][] board, int row, int col, char c){
        for(int i = 0; i < 9; i++) {
            // 檢查第col列,不能有重復(fù)的c
            if(board[i][col] != '.' && board[i][col] == c) {
                return false;
            }
            
            // 檢查第row行,不能有重復(fù)的c
            if(board[row][i] != '.' && board[row][i] == c) {
                return false;
            }
            
            // 檢查對應(yīng)的cube,不能有重復(fù)的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;
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,868評論 0 10
  • The Inner Game of Tennis W Timothy Gallwey Jonathan Cape ...
    網(wǎng)事_79a3閱讀 12,949評論 3 20
  • 好啦,好啦 給你安穩(wěn)的吻 你感覺潮濕嗎 那是因為我愛你啊 所以別擦去,帶著它吧 當(dāng)我變成鹿的時候才能認(rèn)出你。
    梵夜閱讀 225評論 0 0
  • 簡書里面有展臺 五湖四海聚英才 香飄翰墨流芳韻 書法寫字筆下來 自勉 幽雅閑情我自知 春節(jié)將近寫佳詞 詩詞格律填新...
    超生媽媽閱讀 162評論 0 7
  • 走在這樣的路上樂此不疲 嘿,哈,嘿,哈 這樣的聲音 絡(luò)繹不絕 我曾經(jīng)羨慕他們 也想和他們一樣 站在沒有水的南湖旁邊...
    小幽余生不加糖閱讀 167評論 0 0

友情鏈接更多精彩內(nèi)容