ValidSudoku

  • 2022-12-14
  • 浏览 (561)

ValidSudoku.java 源码

package datastructure.array.leetcode;

/**
 * 有效的数独
 */
public class ValidSudoku {

    public boolean isValidSudoku(char[][] board) {
        boolean[][] hor = new boolean[9][9];
        boolean[][] ver = new boolean[9][9];
        boolean[][] grid = new boolean[9][9];
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                if (board[i][j] != '.') {
                    int k = board[i][j] - '1';
                    if (hor[i][k] || ver[j][k] || grid[i / 3 * 3 + j / 3][k]) {
                        return false;
                    }
                    hor[i][k] = true;
                    ver[j][k] = true;
                    grid[i / 3 * 3 + j / 3][k] = true;
                }
            }
        }
        return true;
    }
}

你可能感兴趣的文章

BestTimeBuySellStock

BestTimeBuySellStockII

BullsAndCows

0  赞