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 ...
随机推荐
- Go Example--排序
package main import ( "fmt" "sort" ) func main() { strs := []string{"c" ...
- kafka-producer partitioner.class的使用
partitioner.class的说明 在API客户端中封装好的partition( )方法会为消息选择一个分区编号.为了保证消息负载均衡到每个分区,可以通过使用默认方式或者 手动配置这个参数的 ...
- fges
基本用法: fgets函数用来从文件中读入字符串.fgets函数的调用形式如下:fgets(str,n,fp):此处,fp是文件指针:str是存放在字符串的起始地址:n是一个int类型变量.函数的功能 ...
- 前端 --- 5 BOM 和 DOM
一.BOM BOM(Browser Object Model)是指浏览器对象模型, 它使 JavaScript 有能力与浏览器进行“对话”. 1. window 对象 一些常用的Window方法: ( ...
- Java面向对象 第6节 异常
一.认识异常 异常是指在程序运行过程中所发生的不正常事件,如文件找不到.网络连接不通或链接中断.算数运算出错.数组下标越界.装在一个不存在的类.对null对象操作.类型转换异常等.异常会中断正在运行的 ...
- Http原理与实践
Http原理 一.使用Http协议最简单的例子 1.输入URL打开网页 2.AJAX获取数据 3.img标签加载图片 二.Cache-Control 1.public.private 2.must-r ...
- OpenStack上搭建Q版的公共环境准备(step1)
vmware14 centos7.5minimal版 controller1节点虚拟硬件配置: CPU:1颗2核 Memory:2G 硬盘:20G 网卡: VMnet1(仅主机模式):关闭DHCP,手 ...
- How to load custom styles at runtime (不会翻译,只有抄了 )
原文 :http://blogs.embarcadero.com/sarinadupont/2013/10/16/how-to-load-custom-styles-at-runtime/ How t ...
- MySQL 批量添加
自己封装的一个批量添加. $data 是一个二维数组.key对应是数据表的字段名: /** * 批量创建 * @param array $data * @return int $res 影响行 * @ ...
- 【idea】之使用SVN一些技巧
@Copy https://www.cnblogs.com/whc321/p/5669804.html