36. Valid Sudoku 判断九九有效的数独
[抄题]:
Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:
- Each row must contain the digits
1-9without repetition. - Each column must contain the digits
1-9without repetition. - Each of the 9
3x3sub-boxes of the grid must contain the digits1-9without repetition.
![]()
A partially filled sudoku which is valid.
The Sudoku board could be partially filled, where empty cells are filled with the character '.'
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
不知道怎么判断每个单独的3*3: 三倍行数遍历+一倍列数遍历进入具体的3*3
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[一句话思路]:
每一行都新建三个集合,然后行重复为[i][j] 列重复为[j][i]
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
三倍行数遍历+一倍列数遍历进入具体的3*3
[复杂度]:Time complexity: O(mn) Space complexity: O(mn)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
class Solution {
public boolean isValidSudoku(char[][] board) {
//for loop, judge row, col, and box
for (int i = 0; i < 9; i++) {
//initialization: 3 sets in each new row
HashSet<Character> row = new HashSet<>();
HashSet<Character> col = new HashSet<>();
HashSet<Character> box = new HashSet<>();
for (int j = 0; j < 9; j++) {
//judge row, col
if (board[i][j] != '.' && !row.add(board[i][j])) return false;
if (board[j][i] != '.' && !col.add(board[j][i])) return false;
//judge box
//same for each row
int rowIndex = 3 * (i / 3);
System.out.println("rowIndex = " + rowIndex);
int colIndex = 3 * (i % 3);
System.out.println("colIndex = " + colIndex);
System.out.println("-------------");
//different for each col
if (board[rowIndex + j / 3][colIndex + j % 3] != '.' && !box.add(board[rowIndex + j / 3][colIndex + j % 3])) return false;
}
}
return true;
}
}
36. Valid Sudoku 判断九九有效的数独的更多相关文章
- LeetCode:36. Valid Sudoku,数独是否有效
LeetCode:36. Valid Sudoku,数独是否有效 : 题目: LeetCode:36. Valid Sudoku 描述: Determine if a Sudoku is valid, ...
- leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题
三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Sudoku ...
- 【LeetCode】36. Valid Sudoku 解题报告(Python)
[LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...
- [Leetcode][Python]36: Valid Sudoku
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 36: Valid Sudokuhttps://oj.leetcode.com ...
- [LeetCode] 36. Valid Sudoku 验证数独
Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to th ...
- leetCode 36.Valid Sudoku(有效的数独) 解题思路和方法
Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...
- LeetCode 36 Valid Sudoku
Problem: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board ...
- 蜗牛慢慢爬 LeetCode 36.Valid Sudoku [Difficulty: Medium]
题目 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- 36. Valid Sudoku
============= Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku b ...
随机推荐
- Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable)
To fix it, just kill the apt process. ps -aux | grep -i apt sudo kill -9 "the process ID"
- spring IOC 和AOP 方面
spring 的2大核心 是Ioc 和 aop spring的依赖注入:在程序运行期间,由外部容器动态的将依赖对象注入到组件中 IOC: 实例化spring容器的二种方法 第一种:在类路径下寻找配 ...
- LOJ 2292 「THUSC 2016」成绩单——区间DP
题目:https://loj.ac/problem/2292 直接 DP 很难做,主要是有那种 “一个区间内部有很多个别的区间” 的情况. 自己想了一番枚举 max-min 的最大限制,然后在该基础上 ...
- [转]使用Cython来保护Python代码库
转自:http://blog.csdn.net/chenyulancn/article/details/77168621 最近,我在做一个需要使用Cython来保护整个代码库的Python项目. 起初 ...
- 使用nrm工具高效地管理npm源
在使用npm时,官方的源下载npm包会比较慢,国内我们基本使用淘宝的源,如果公司内部搭建了一套npm私有仓库,公司内部的源不可能把npm官方的npm包都同步,所以需要切换npm源.如果使用npm/cn ...
- java中的内部类详解
https://www.cnblogs.com/dolphin0520/p/3811445.html https://www.cnblogs.com/chenssy/p/3388487.html
- Bootstrap 插件收集
Bootstrap-Mutilselect 将下拉选项扩展支持多选以及多种选择方式 http://davidstutz.de/bootstrap-multiselect/ Bootstrap Sel ...
- docker network基础
前面介绍了nginx与php两个容器间是如何进行通信的: [root@docker ~]# docker run -d --name=php -v /www:/usr/local/nginx/html ...
- (整理)SQL Server 2008 CDC 功能使用
最近某项目突然要增加数据的获取,但是不能改程序.也没有同步的只读库,只好使用CDC来进行尝试. CDC的启用和停止全部用SQL实现,在这里给出主要的SQL步骤: /****** Script for ...
- 成为JavaGC专家—深入浅出Java垃圾回收机制
对于Java开发人员来说,了解垃圾回收机制(GC)有哪些好处呢? 首先可以满足作为一名软件工程师的求知欲, 其次,深入了解GC如何工作可以帮你写出更好的Java应用. 这仅仅代表我个人的意见,但我坚信 ...