Valid Sudoku

来源:互联网 发布:淘宝怎么看以前的评价 编辑:程序博客网 时间:2024/05/17 20:01

比以前有进步

public class Solution {    public boolean isValidSudoku(char[][] board) {        if (board == null || board.length == 0 || board[0].length == 0) {            return true;        }        for (int i = 0; i < board.length; i++) {            Set<Character> set = new HashSet<>();            for (int j = 0; j < board[0].length; j++) {                if (board[i][j] == '.') {                    continue;                }                if (!set.add(board[i][j])) {                    return false;                }            }         }        for (int i = 0; i < board[0].length; i++) {            Set<Character> set = new HashSet<>();            for (int j = 0; j < board.length; j++) {                if (board[j][i] == '.') {                    continue;                }                if (!set.add(board[j][i])) {                    return false;                }            }         }        for (int i = 0; i < board.length; i = i + 3) {            for (int j = 0; j < board[0].length; j = j + 3) {                Set<Character> set = new HashSet<>();                for (int x = i; x < i + 3; x++) {                    for (int y = j; y < j + 3; y++) {                        if (board[x][y] == '.') {                            continue;                        }                        if (!set.add(board[x][y])) {                            return false;                        }                    }                 }            }        }        return true;    }}


0 0
原创粉丝点击