其他分享
首页 > 其他分享> > LeetCode 36. Valid Sudoku

LeetCode 36. Valid Sudoku

作者:互联网

题目来源:https://leetcode.com/problems/valid-sudoku/

问题描述

36. Valid Sudoku

Medium

Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:

  1. Each row must contain the digits 1-9 without repetition.
  2. Each column must contain the digits 1-9 without repetition.
  3. Each of the 9 3x3 sub-boxes of the grid must contain the digits 1-9 without repetition.

The Sudoku board could be partially filled, where empty cells are filled with the character '.'.

Example 1:

Input:

[

  ["5","3",".",".","7",".",".",".","."],

  ["6",".",".","1","9","5",".",".","."],

  [".","9","8",".",".",".",".","6","."],

  ["8",".",".",".","6",".",".",".","3"],

  ["4",".",".","8",".","3",".",".","1"],

  ["7",".",".",".","2",".",".",".","6"],

  [".","6",".",".",".",".","2","8","."],

  [".",".",".","4","1","9",".",".","5"],

  [".",".",".",".","8",".",".","7","9"]

]

Output: true

Example 2:

Input:

[

  ["8","3",".",".","7",".",".",".","."],

  ["6",".",".","1","9","5",".",".","."],

  [".","9","8",".",".",".",".","6","."],

  ["8",".",".",".","6",".",".",".","3"],

  ["4",".",".","8",".","3",".",".","1"],

  ["7",".",".",".","2",".",".",".","6"],

  [".","6",".",".",".",".","2","8","."],

  [".",".",".","4","1","9",".",".","5"],

  [".",".",".",".","8",".",".","7","9"]

]

Output: false

Explanation: Same as Example 1, except with the 5 in the top left corner being

    modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid.

Note:

------------------------------------------------------------

题意

给定一个升序序列nums和一个数target,如果target在nums中则返回target的位置,否则返回target应该插入的位置。

------------------------------------------------------------

思路

暴力遍历判断即可。

------------------------------------------------------------

代码

class Solution {
    public static final int[] BLOCK_X = {0, 0, 0, 3, 3, 3, 6, 6, 6};
    public static final int[] BLOCK_Y = {0, 3, 6, 0, 3, 6, 0, 3, 6};   
    public boolean[] digits = new boolean[9];
    
    public boolean isValidSudoku(char[][] board) {
        int i = 0, j = 0;
        char ch = 0;
        int val = -1;
        // check rows
        for (i=0; i<9; i++)
        {
            Arrays.fill(digits, false); 
            for (j=0; j<9; j++)
            {
                ch = board[i][j];
                if (ch == '.')
                {
                    continue;
                }
                else
                {
                    val = Character.getNumericValue(ch) - 1;
                    if (digits[val])
                    {
                        return false;
                    }
                    else
                    {
                        digits[val] = true;
                    }
                }
            }
        }
        // check columns
        for (i=0; i<9; i++)
        {
            Arrays.fill(digits, false); 
            for (j=0; j<9; j++)
            {
                ch = board[j][i];
                if (ch == '.')
                {
                    continue;
                }
                else
                {
                    val = Character.getNumericValue(ch) - 1;
                    if (digits[val])
                    {
                        return false;
                    }
                    else
                    {
                        digits[val] = true;
                    }
                }
            }
        }
        // check 3x3 blocks
        for (int block = 0; block < 9; block++)
        {
            Arrays.fill(digits, false); 
            for (i=0; i<3; i++)
            {
                for (j=0; j<3; j++)
                {
                    ch = board[BLOCK_X[block]+i][BLOCK_Y[block]+j];
                    if (ch == '.')
                    {
                        continue;
                    }
                    else
                    {
                        val = Character.getNumericValue(ch) - 1;
                        if (digits[val])
                        {
                            return false;
                        }
                        else
                        {
                            digits[val] = true;
                        }
                    }
                }
            }
        }
        return true;
    }
}

 

标签:digits,Sudoku,public,Valid,board,contain,LeetCode,filled
来源: https://blog.csdn.net/da_kao_la/article/details/88774898