[leetcode-36-Valid Sudoku]

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.

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

A partially filled sudoku which is valid.

Note:
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.

思路:

分别用三个二维数组分别记录行、列和子数独里面数字的使用情况。一旦发现已经有数字之前被占用了,返回false。

其中需要注意的是,子数独的下标,如上图中一共存在3*3个子数独,每行3个,一共3行。对于大的数独索引(i,j),对应到

子数独的下标为:i/3 * 3 + j/3 。(比如,对于一个二维m*n的数组,坐标(a,b)映射到一维坐标为:所在行a乘以列数n+列坐标b 即a*n+b)

这里i/3 * 3 + j/3相当于第i/3行,j/3列。

如下是参考大神写的:

参考:

bool isValidSudoku(vector<vector<char>>& board)
 {
     int rowcheck[9][9] = {0},colcheck[9][9] = {0},subboxcheck[9][9] = {0};
     for(int i = 0;i<board.size();i++)
     {
         for(int j =0 ;j<board[0].size();j++)
         {
             if(board[i][j] != ‘.‘)
             {
                 int num = board[i][j] - ‘0‘ - 1,k = i/3 * 3 + j/3;//记录子数独的下标一共3*3个子数独 m*n的数组 (a,b) 一维下标为a*n+b
                 if(rowcheck[i][num] || colcheck[j][num] || subboxcheck[k][num])return false;
                 rowcheck[i][num] = colcheck[j][num]  = subboxcheck[k][num] = 1;
             }
         }
     }
     return true;
}

https://discuss.leetcode.com/topic/8241/my-short-solution-by-c-o-n2/2

时间: 2024-11-18 01:53:54

[leetcode-36-Valid Sudoku]的相关文章

leetCode 36. Valid Sudoku(数独) 哈希

36. Valid Sudoku(合法数独) Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. Note:A valid S

leetCode 36.Valid Sudoku(有效的数独) 解题思路和方法

Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. Note: A valid Sudoku boa

LeetCode 36 Valid Sudoku (C,C++,Java,Python)

Problem: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. Note: A valid Sudoku board (

LeetCode 36 Valid Sudoku(合法的数独)

题目链接: https://leetcode.com/problems/valid-sudoku/?tab=Description 给出一个二维数组,数组大小为数独的大小,即9*9 其中,未填入数字的数组值为’.’ 判断当前所给已知数组中所填的数字是否合法. 数独合法性判断: 1. 满足每一行的数字都只能是1~9,并且不能产生重复 2. 满足每一列的数字都只能是1~9,并且不能产生重复 3. 满足每一个3*3的正方形块中的数字只能是1~9,并且不能产生重复 判断过程: 初始化三个数组:row,

[LeetCode] 036. Valid Sudoku (Easy) (C++)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 036. Valid Sudoku (Easy) 链接: 题目:https://leetcode.com/problems/valid-sudoku/ 代码(github):https://github.com/illuz/leetcode 题意: 判断一个数独是否有效. 有效的数独不强求有解. 分析: 只要同一行

[Leetcode][Python]36: Valid Sudoku

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 36: Valid Sudokuhttps://oj.leetcode.com/problems/valid-sudoku/ Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty

【LeetCode】36 - Valid Sudoku

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.(http://sudoku.com.au/TheRules.aspx) The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. N

[LeetCode][JavaScript]Valid Sudoku

https://leetcode.com/problems/valid-sudoku/ Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sud

[email&#160;protected] [36] Valid Sudoku

https://leetcode.com/problems/valid-sudoku/ Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is

【LeetCode】Valid Sudoku

Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. Note:A valid Sudoku boar