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 '.'.

250px-Sudoku-by-L2G-20050714.svg.png

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.

关于数独的简介:

There are just 3 rules to Sudoku.

1.Each row must have the numbers 1-9 occuring just once.

horizontal.jpg

2.Each column must have the numbers 1-9 occuring just once.

vertical.jpg

3.And the numbers 1-9 must occur just once in each of the 9 sub-boxes of the grid.

square.jpg

题目大意:

判断一个给定的二维数组是否是一个合法的数独矩阵。

思路:

采用set这一容器,来进行去重。

1.判断每一行是否合法。

2.判断每一列是否合法。

3.判断每一个九宫格是否合法。

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
class  Solution {
public :
     bool  isValidSudoku(vector<vector< char >>& board) 
     {
         set< char > mySet;
         //1.判断每一行是否合法
         for  ( int  row = 0; row < 9; row++)
         {
             //cout<<"检测行:"<<row<<endl;
             for  ( int  column = 0; column < 9; column++)
             {
                 if  (board[row][column] ==  '.' )
                 {
                     continue ;
                 }
                 if  (mySet.find(board[row][column]) == mySet.end())
                 {
                     mySet.insert(board[row][column]);
                 }
                 else
                 {
                     return  false ;
                 }
             }
             mySet.clear();
         }
         
         //2.判断每一列是否合法
         for  ( int  row = 0; row < 9; row++)
         {
             //cout<<"检测列:"<<row<<endl;
             for  ( int  column = 0; column < 9; column++)
             {
                 if  (board[column][row] ==  '.' )
                 {
                     continue ;
                 }
                 if  (mySet.find(board[column][row]) == mySet.end())
                 {
                     mySet.insert(board[column][row]);
                 }
                 else
                 {
                     return  false ;
                 }
             }
             mySet.clear();
         }
     
         //3.判断每一个九宫格是否合法
         for  ( int  row = 0; row < 9; row += 3)
         {
             for  ( int  column = 0; column < 9; column += 3)
             {
                 for  ( int  i = row; i < row + 3; i++)
                 {
                     for  ( int  j = column; j < column + 3; j++)
                     {
                         if  (board[i][j] ==  '.' )
                         {
                             continue ;
                         }
                         if  (mySet.find(board[i][j]) == mySet.end())
                         {
                             mySet.insert(board[i][j]);
                         }
                         else
                         {
                             return  false ;
                         }
                     }
                 }
                 mySet.clear();
             }
         }
         return  true ;
     }
};


本文转自313119992 51CTO博客,原文链接:http://blog.51cto.com/qiaopeng688/1837537
相关文章
|
4月前
《LeetCode》—— 哈希
《LeetCode》—— 哈希
|
4月前
leetcode-1044:最长重复子串(滚动哈希)
leetcode-1044:最长重复子串(滚动哈希)
77 0
|
4月前
|
Java
leetcode-37:解数独
leetcode-37:解数独
37 0
|
26天前
|
存储 算法 索引
LeetCode第36题有效的数独
这篇文章介绍了LeetCode第36题"有效的数独"的解题方法,通过使用三个二维数组分别记录每一行、每一列以及每个3x3宫格内1-9数字出现的次数,来验证给定数独是否有效。
|
3月前
|
存储 SQL 算法
LeetCode 题目 65:有效数字(Valid Number)【python】
LeetCode 题目 65:有效数字(Valid Number)【python】
|
3月前
|
算法 数据挖掘 开发者
LeetCode题目55:跳跃游戏【python5种算法贪心/回溯/动态规划/优化贪心/索引哈希映射 详解】
LeetCode题目55:跳跃游戏【python5种算法贪心/回溯/动态规划/优化贪心/索引哈希映射 详解】
|
3月前
|
算法
力扣经典150题解析之三十四:有效的数独
力扣经典150题解析之三十四:有效的数独
23 0
|
3月前
|
算法
【经典LeetCode算法题目专栏分类】【第3期】回溯问题系列:单词搜索、N皇后问题、判断有效数独、解数独
【经典LeetCode算法题目专栏分类】【第3期】回溯问题系列:单词搜索、N皇后问题、判断有效数独、解数独
|
3月前
|
存储 SQL 算法
LeetCode 题目 87:递归\动态规划\哈希实现 扰乱字符串
LeetCode 题目 87:递归\动态规划\哈希实现 扰乱字符串
|
4月前
[leetcode] 705. 设计哈希集合
[leetcode] 705. 设计哈希集合