第七篇 Valid Sudoku

Uber 高频题,细节实现,没什么太多可说的

注意检查3*3 square可能会比较难想

 1 public boolean isValidSudoku(char[][] board) {
 2         if (board == null || board.length == 0) {
 3             return false;
 4         }
 5         //O(MN) time
 6         //O(N) space
 7         int n = board.length;
 8         int m = board[0].length;
 9         HashSet<Character> set = new HashSet();
10         //Check rows
11         for (int i = 0; i < n; i++) {
12             for (int j = 0; j < m; j++) {
13                 if (board[i][j] != ‘.‘) {
14                     if (set.contains(board[i][j])) {
15                         //System.out.println("Row false " + board[i][j]);
16                         return false;
17                     }
18                     set.add(board[i][j]);
19                 }
20             }
21             set.clear();
22         }
23         set.clear();
24         //Check columns
25         for (int i = 0; i < m; i++) {
26             for (int j = 0; j < n; j++) {
27                 if (board[j][i] != ‘.‘) {
28                     if (set.contains(board[j][i])) {
29                         //System.out.println("Column false " + board[j][i]);
30                         return false;
31                     }
32                     set.add(board[j][i]);
33                 }
34             }
35             set.clear();
36         }
37         set.clear();
38         //Check squares
39         for (int i = 0; i < n - 2; i += 3) {
40             for (int j = 0; j < m - 2; j += 3) {
41                 for (int k = 0; k < 9; k ++) {
42                     if (board[i + k / 3][j + k % 3] != ‘.‘) {
43                         if (set.contains(board[i + k / 3][j + k % 3])) {
44                             return false;
45                         }
46                         set.add(board[i + k / 3][j + k % 3]);
47                     }
48                 }
49                 set.clear();
50             }
51         }
52         return true;
53     }
时间: 2024-10-26 06:58:23

第七篇 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 board (partially

SaltStack 入门到精通 - 第七篇: Targeting

什么是Targeting? Targeting minions 是指那些minion会作为运行命令或是执行状态的目标.这些目标可以是一个主机名,系统信息,定义的分组,甚至是自定义的绑定的对象. 例如命令  salt web1 apache.signal restart 可以重启ID 为web1的minion的apache.当然也可以在top文件中使用web1来作为目标匹配的内容: base:   'web1':     - webserver Targing 有哪些匹配方式? Minion Id

[LeetCode]Valid Sudoku

检测数独是否合格. 思路: 填充一遍就知道是否合格. 基本暴力搜索的思想. 1 /*************************************************************************************************** 2 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. 3 The Sudoku board could be parti

Python之路【第七篇】:线程、进程和协程

Python之路[第七篇]:线程.进程和协程 Python线程 Threading用于提供线程相关的操作,线程是应用程序中工作的最小单元. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #!/usr/bin/env python # -*- coding:utf-8 -*- import threading import time   def show(arg):     time.sleep(1)     print 'thread'+str(arg)   for i in

leetcode笔记: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 '.'. The following figure: A partially f

【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

用仿ActionScript的语法来编写html5——第七篇,自定义按钮

第七篇,自定义按钮 这次弄个简单点的,自定义按钮.其实,有了前面所定义的LSprite,LBitmap等类,定义按钮就很方便了.下面是添加按钮的代码, function gameInit(event){ backLayer = new LSprite(); addChild(backLayer); btn01 = new LButton(new LBitmap(new LBitmapData(imglist["replay_button_up"])),new LBitmap(new L

第七篇 Integration Services:中级工作流管理

本篇文章是Integration Services系列的第七篇,详细内容请参考原文. 简介在上一篇文章,我们创建了一个新的SSIS包,学习了SSIS中的脚本任务和优先约束,并检查包的MaxConcurrentExecutables属性.这一篇我们将检查.演示并测试优先约束赋值为"成功"."完成"."失败"时对工作流的影响.约束赋值如果你按照前一篇的步骤操作过,打开My_First_SSIS_Project解决方案下的Precedence.dtsx

【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