【LeetCode 36_哈希表】Valid Sudoku

 1 //occupyed_1检查行是否占用
 2 //occupyed_2检查列是否占用
 3 //occupyed_3检查块是否占用
 4 bool isValidSudoku(vector<vector<char>>& board)
 5 {
 6     int occupyed_1[9][9], occupyed_2[9][9], occupyed_3[9][9];
 7     for (int i = 0; i < 9; ++i) {
 8         for (int j = 0; j < 9; ++j) {
 9             occupyed_1[i][j] = 0;
10             occupyed_2[i][j] = 0;
11             occupyed_3[i][j] = 0;
12         }
13     }
14
15     int rowSize = board.size();
16     int colSize = board[0].size();
17     for (int i = 0; i < rowSize; ++i) {
18         for (int j = 0; j < colSize; ++j) {
19             if (board[i][j] != ‘.‘) {
20                 int num = board[i][j] - ‘0‘ - 1;
21                 int k = i / 3 * 3 + j / 3;
22
23                 if (occupyed_1[i][num] || occupyed_2[j][num]
24                     || occupyed_3[k][num])
25                     return false;
26                 occupyed_1[i][num] = 1;
27                 occupyed_2[j][num] = 1;
28                 occupyed_3[k][num] = 1;
29             }
30         }
31     }
32     return true;
33 }
时间: 2024-10-25 19:40:18

【LeetCode 36_哈希表】Valid Sudoku的相关文章

LeetCode之“散列表”: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

【leetcode刷题笔记】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

【LeetCode】哈希表 hash_table(共88题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [1]Two Sum (2018年11月9日,k-sum专题,算法群衍生题) 给了一个数组 nums, 和一个 target 数字,要求返回一个下标的 pair, 使得这两个元素相加等于 target . 题解:我这次最大范围的优化代码, hash-table + one pass,时间复杂度 O(N),空间复杂度 O(N).重点在于动态找,一边生成hash-tabl

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][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

[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] 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: Valid Sudoku [035]

[题目] 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 (part

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 (partia