LeetCode //C - 36. Valid Sudoku

36. Valid Sudoku

Determine if a 9 x 9 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 nine 3 x 3 sub-boxes of the grid must contain the digits 1-9 without repetition.

Note:

  • A Sudoku board (partially filled) could be valid but is not necessarily solvable.
  • Only the filled cells need to be validated according to the mentioned rules.

Example 1:

Input: board =

\["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: board =

\["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.

Constraints:

  • board.length == 9
  • boardi.length == 9
  • boardij is a digit 1-9 or '.'.

From: LeetCode

Link: 36. Valid Sudoku


Solution:

Ideas:
  • Iterate through each cell in the board.
  • For each non-empty cell, check if its value violates the Sudoku rules.
  • The Sudoku rules require that each number (1-9) must not repeat in the same row, the same column, or the same 3x3 box.
  • We can use three 9x9 boolean matrices to track the existence of numbers in rows, columns, and boxes. The first matrix, rows, tracks numbers in each row, the second matrix, cols, tracks numbers in each column, and the third matrix, boxes, tracks numbers in each box.
  • If we find a number that has already existed in the current row, column, or box, return false. If we successfully go through the board without finding any duplicates, return true.
Code:
c 复制代码
bool isValidSudoku(char** board, int boardSize, int* boardColSize){
    bool rows[9][9] = {0};
    bool cols[9][9] = {0};
    bool boxes[9][9] = {0};

    for(int i = 0; i < 9; i++){
        for(int j = 0; j < 9; j++){
            if(board[i][j] != '.'){
                int num = board[i][j] - '1'; // subtract '1' to fit in 0-8 index
                int box_index = (i / 3) * 3 + j / 3; // map the box position to the box index
                
                // Check if this value has already been recorded in the current row, column, or box
                if(rows[i][num] || cols[j][num] || boxes[box_index][num]){
                    return false;
                }
                
                // Record the existence of the number in the current row, column, and box
                rows[i][num] = true;
                cols[j][num] = true;
                boxes[box_index][num] = true;
            }
        }
    }

    return true;
}
相关推荐
bIo7lyA8v3 分钟前
算法可视化对教学与调试效率的影响分析的技术8
算法
郝学胜-神的一滴9 分钟前
CMake 017:彩色日志输出实战
linux·c语言·开发语言·c++·软件工程·软件构建·cmake
hunterkkk(c++)12 分钟前
优先队列启发式最短路径快速算法(优化SPFA)-HEAP_SPFA算法
算法
SiliconGazer25 分钟前
第15届国赛满分代码解析(下)—— 运动轨迹算法、按键交互与完整状态机
算法·状态机·stc15f2k60s2·浮点运算·蓝桥杯国赛·运动轨迹、·向量分解
Navigator_Z26 分钟前
LeetCode //C - 1096. Brace Expansion II
c语言·算法·leetcode
luj_176828 分钟前
FreeDOS vs MS-DOS PC-DOS 对比解析
服务器·c语言·开发语言·经验分享·算法
笨笨没好名字42 分钟前
Leetcode刷题python版第一周
python·算法·leetcode
Cthy_hy1 小时前
斯特林数:组合划分的递归经典,一二两类全解
python·算法·斯特林数
不忘不弃1 小时前
计算pi的近似值
算法
码云骑士1 小时前
12-GIL不是性能杀手(下)-绕过GIL的三种方案与决策树
算法·决策树·机器学习