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
  • board[i].length == 9
  • board[i][j] 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;
}
相关推荐
杜子不疼.5 分钟前
Ascend_C自定义算子开发
c语言·开发语言
小乔的编程内容分享站9 分钟前
C语言笔记之函数
c语言·笔记
R1nG86325 分钟前
CANN资源泄漏检测工具源码深度解读 实战设备内存泄漏排查
数据库·算法·cann
_OP_CHEN34 分钟前
【算法基础篇】(五十六)容斥原理指南:从集合计数到算法实战,解决组合数学的 “重叠难题”!
算法·蓝桥杯·c/c++·组合数学·容斥原理·算法竞赛·acm/icpc
杜子不疼.37 分钟前
基于ATVC模板库的Ascend C Vector算子快速开发指南
c语言·开发语言·mfc
TracyCoder1231 小时前
LeetCode Hot100(27/100)——94. 二叉树的中序遍历
算法·leetcode
九.九1 小时前
CANN HCOMM 底层机制深度解析:集合通信算法实现、RoCE 网络协议栈优化与多级同步原语
网络·网络协议·算法
C++ 老炮儿的技术栈1 小时前
Qt Creator中不写代如何设置 QLabel的颜色
c语言·开发语言·c++·qt·算法
艾莉丝努力练剑1 小时前
【Linux:文件】基础IO
linux·运维·c语言·c++·人工智能·io·文件
you-_ling1 小时前
IO编程相关知识
c语言·vscode