标签:date dex ida https 利用 int bar mat 除了
判断一个 9x9 的数独是否有效。只需要根据以下规则,验证已经填入的数字是否有效即可。
1-9
在每一行只能出现一次。1-9
在每一列只能出现一次。1-9
在每一个以粗实线分隔的 3x3
宫内只能出现一次。上图是一个部分填充的有效的数独。
数独部分空格内已填入了数字,空白格用 ‘.‘
表示。
示例 1:
输入: [ ["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"] ] 输出: true
示例 2:
输入: [ ["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"] ] 输出: false 解释: 除了第一行的第一个数字从 5 改为 8 以外,空格内其他数字均与 示例1 相同。 但由于位于左上角的 3x3 宫内有两个 8 存在, 因此这个数独是无效的。
说明:
1-9
和字符 ‘.‘
。9x9
形式的。一个简单的解决方案是遍历该 9 x 9
数独 三 次,以确保:
3 x 3
子数独内没有重复的数字。实际上,所有这一切都可以在一次迭代中完成。
首先,让我们来讨论下面两个问题:
可以使用
box_index = (row / 3) * 3 + columns / 3
,其中/
是整数除法。
可以利用
value -> count
哈希映射来跟踪所有已经遇到的值。
现在,我们完成了这个算法的所有准备工作:
false
。true
。class Solution {
public boolean isValidSudoku(char[][] board) {
// init data
HashMap<Integer, Integer> [] rows = new HashMap[9];
HashMap<Integer, Integer> [] columns = new HashMap[9];
HashMap<Integer, Integer> [] boxes = new HashMap[9];
for (int i = 0; i < 9; i++) {
rows[i] = new HashMap<Integer, Integer>();
columns[i] = new HashMap<Integer, Integer>();
boxes[i] = new HashMap<Integer, Integer>();
}
<span class="hljs-comment">// validate a board</span>
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < <span class="hljs-number">9</span>; i++) {
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> j = <span class="hljs-number">0</span>; j < <span class="hljs-number">9</span>; j++) {
<span class="hljs-keyword">char</span> num = board[i][j];
<span class="hljs-keyword">if</span> (num != <span class="hljs-string">‘.‘</span>) {
<span class="hljs-keyword">int</span> n = (<span class="hljs-keyword">int</span>)num;
<span class="hljs-keyword">int</span> box_index = (i / <span class="hljs-number">3</span> ) * <span class="hljs-number">3</span> + j / <span class="hljs-number">3</span>;
<span class="hljs-comment">// keep the current cell value</span>
rows[i].put(n, rows[i].getOrDefault(n, <span class="hljs-number">0</span>) + <span class="hljs-number">1</span>);
columns[j].put(n, columns[j].getOrDefault(n, <span class="hljs-number">0</span>) + <span class="hljs-number">1</span>);
boxes[box_index].put(n, boxes[box_index].getOrDefault(n, <span class="hljs-number">0</span>) + <span class="hljs-number">1</span>);
<span class="hljs-comment">// check if this value has been already seen before</span>
<span class="hljs-keyword">if</span> (rows[i].get(n) > <span class="hljs-number">1</span> || columns[j].get(n) > <span class="hljs-number">1</span> || boxes[box_index].get(n) > <span class="hljs-number">1</span>)
<span class="hljs-keyword">return</span> <span class="hljs-keyword">false</span>;
}
}
}
<span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>;
}
}
class Solution:
def isValidSudoku(self, board):
"""
:type board: List[List[str]]
:rtype: bool
"""
# init data
rows = [{} for i in range(9)]
columns = [{} for i in range(9)]
boxes = [{} for i in range(9)]
<span class="hljs-comment"># validate a board</span>
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(<span class="hljs-number">9</span>):
<span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> range(<span class="hljs-number">9</span>):
num = board[i][j]
<span class="hljs-keyword">if</span> num != <span class="hljs-string">‘.‘</span>:
num = int(num)
box_index = (i // <span class="hljs-number">3</span> ) * <span class="hljs-number">3</span> + j // <span class="hljs-number">3</span>
<span class="hljs-comment"># keep the current cell value</span>
rows[i][num] = rows[i].get(num, <span class="hljs-number">0</span>) + <span class="hljs-number">1</span>
columns[j][num] = columns[j].get(num, <span class="hljs-number">0</span>) + <span class="hljs-number">1</span>
boxes[box_index][num] = boxes[box_index].get(num, <span class="hljs-number">0</span>) + <span class="hljs-number">1</span>
<span class="hljs-comment"># check if this value has been already seen before</span>
<span class="hljs-keyword">if</span> rows[i][num] > <span class="hljs-number">1</span> <span class="hljs-keyword">or</span> columns[j][num] > <span class="hljs-number">1</span> <span class="hljs-keyword">or</span> boxes[box_index][num] > <span class="hljs-number">1</span>:
<span class="hljs-keyword">return</span> <span class="hljs-literal">False</span>
<span class="hljs-keyword">return</span> <span class="hljs-literal">True</span>
复杂度分析
81
个单元格进行了一次迭代。https://www.jianshu.com/p/ad0311d97760
标签:date dex ida https 利用 int bar mat 除了
原文地址:https://www.cnblogs.com/leetcodetijie/p/13195163.html