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 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.
2.Each column must have the numbers 1-9 occuring just once.
3.And the numbers 1-9 must occur just once in each of the 9 sub-boxes of the grid.
題目大意:
判斷一個給定的二維陣列是否是一個合法的數獨矩陣。
思路:
採用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
相關文章
- Leetcode 36 Valid SudokuLeetCode
- 036 Valid Sudoku
- sudoku 數獨 XY-ChainsAI
- 2024.9.4 leetcode 169 多數元素 (雜湊表)LeetCode
- Leetcode 37 Sudoku SolverLeetCode
- 【Leetcode_Hot100】雜湊LeetCode
- 2024/12/6 【雜湊表】LeetCode1.兩數之和 【√】LeetCode
- LeetCode705.設計雜湊集合LeetCode
- 「LeetCode Top100」之雜湊篇LeetCode
- leetcode 1525 字串的好分割數目(雜湊表,字串分割)LeetCode字串
- 雜湊求眾數
- Hash,雜湊,雜湊?
- 2024.9.6 leetcode 509 斐波那契數 (雜湊表/動態規劃)LeetCode動態規劃
- 七夕也要學起來,雜湊雜湊雜湊!
- 雜湊查詢 兩數之和
- Leetcode 20 Valid ParenthesesLeetCode
- 【LeetCode1】【雜湊】每日一題 day30LeetCode每日一題
- 雜湊
- LeetCode65. Valid Number — 判斷合法數字LeetCode
- 雜湊表(雜湊表)原理詳解
- 【尋跡#3】 雜湊與雜湊表
- js 雜湊雜湊值的模組JS
- leetcode:有效的數獨LeetCode
- Leetcode 32 Longest Valid ParenthesesLeetCode
- 《雜湊表》242. 有效的字母異位詞《leetcode》LeetCode
- 14-1 雜湊表基礎 / Leetcode first uniq charLeetCode
- ACM金牌選手講解LeetCode演算法《雜湊》ACMLeetCode演算法
- 2024.9.6 leetcode 1137 第 N 個泰波那契數 (雜湊表/動態規劃)LeetCode動態規劃
- 雜湊索引索引
- 樹雜湊
- 2024.3.26 雜湊
- 雜湊碰撞
- 字串雜湊字串
- 雜湊表
- 圖解兩數之和:雜湊表法圖解
- LeetCode1002. 查詢常用字元(雜湊表、count)LeetCode字元
- 2024.9.6 leetcode 70 爬樓梯 (雜湊表/動態規劃)LeetCode動態規劃
- LeetCode 36——有效的數獨LeetCode
- LeetCode 37. 解數獨LeetCode