LeetCode:36. Valid Sudoku(Medium)
1. 原题链接
https://leetcode.com/problems/valid-sudoku/description/
2. 题目要求
给定一个 9✖️9 的数独,判断该数独是否合法
数独用字符类型的二维数组表示,为空的地方用 '.' 代替
合法应满足以下要求:(1)每一列的数字不重复;(2)每一行的数字不重复;(3)3✖️3区域内不存在重复值;下图是一个合法的数独:

3. 解题思路
根据合法应满足的三个要求依此来进行判断:
(1)行和列是否存在重复值:可以通过两层for循环遍历二维数组,然后使用HashSet,因为HashSet不允许存在重复值。依此将遍历到的数字加入HashSet,无法加入时则证明存在重复,返回false;
(2)3✖️3区域内是否存在重复值:
难点:如何经过一次内部for循环就能遍历到一个 3✖️3 区域

假设我们现在要得到红框圈住的3✖️3区域,第一层for循环 i 此时为“1”,第二层for循环 j 从“0”遍历到“8”。如何确保rowIndex和columnIndex的取值范围都是 1~3?
可以发现 “j/3” 和 “j%3”的范围是 0~2,此时的 i =1,因此我们要对这三者进行利用,就能保证rowIndex和columnIndex的取值范围都是 1~3。
4. 代码实现
import java.util.HashSet;
public class ValidSudoku36 {
public static void main(String[] args) {
char[][] board = {{'', '', '.', '.', '', '.', '.', '.', '.'},
{'', '.', '.', '', '', '', '.', '.', '.'},
{'.', '', '', '.', '.', '.', '.', '', '.'},
{'', '.', '.', '.', '', '.', '.', '.', ''},
{'', '.', '.', '', '.', '', '.', '.', ''},
{'', '.', '.', '.', '', '.', '.', '.', ''},
{'.', '', '.', '.', '.', '.', '', '', '.'},
{'.', '.', '.', '', '', '', '.', '.', ''},
{'.', '.', '.', '.', '', '.', '.', '', ''}};
System.out.println(isValidSudoku(board));
}
public static boolean isValidSudoku(char[][] board) {
for (int i = ; i < board.length; i++) {
HashSet<Character> row = new HashSet<Character>();
HashSet<Character> column = new HashSet<Character>();
HashSet<Character> cube = new HashSet<Character>();
for (int j = ; j < board.length; j++) {
if (board[i][j] != '.' && !row.add(board[i][j]))
return false;
if (board[j][i] != '.' && !column.add(board[j][i]))
return false;
int rowIndex = * (i / );
int columnIndex = * (i % );
System.out.print(rowIndex + j / +",");
System.out.print(columnIndex + j % +" ");
if (board[rowIndex + j / ][columnIndex + j % ] != '.' && !cube.add(board[rowIndex + j / ][columnIndex + j % ]))
return false;
}
System.out.println("");
System.out.println("");
}
return true;
}
}
LeetCode:36. Valid Sudoku(Medium)的更多相关文章
- LeetCode:39. Combination Sum(Medium)
1. 原题链接 https://leetcode.com/problems/combination-sum/description/ 2. 题目要求 给定一个整型数组candidates[ ]和目标值 ...
- LeetCode:43. Multiply Strings (Medium)
1. 原题链接 https://leetcode.com/problems/multiply-strings/description/ 2. 题目要求 给定两个String类型的正整数num1.num ...
- 【leetcode】36. Valid Sudoku(判断能否是合法的数独puzzle)
Share Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated accordi ...
- LeetCode:16. 3Sum Closest(Medium)
1. 原题链接 https://leetcode.com/problems/3sum-closest/description/ 2. 题目要求 数组S = nums[n]包含n个整数,找出S中三个整数 ...
- LeetCode:49. Group Anagrams(Medium)
1. 原题链接 https://leetcode.com/problems/group-anagrams/description/ 2. 题目要求 给定一个字符串数组,将数组中包含相同字母的元素放在同 ...
- LeetCode:22. Generate Parentheses(Medium)
1. 原题链接 https://leetcode.com/problems/generate-parentheses/description/ 2. 题目要求 给出一个正整数n,请求出由n对合法的圆括 ...
- LeetCode:20. Valid Parentheses(Easy)
1. 原题链接 https://leetcode.com/problems/valid-parentheses/description/ 2. 题目要求 给定一个字符串s,s只包含'(', ')', ...
- LeetCode:9. Palindromic Number(Medium)
原题链接:https://leetcode.com/problems/palindrome-number/description/ 1. 题目要求:判断一个int类型整数是否是回文,空间复杂度O(1) ...
- 【LeetCode】36. Valid Sudoku 解题报告(Python)
[LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...
随机推荐
- Python Django 分页
Python Django 分页 http://www.360doc.com/content/14/0721/17/16044571_396090985.shtml
- Django 按时间来查找数据库中的数据
问题: 按时间来查找数据表中的数据. 前提: 1. 数据表student中有一个字段类型为DateField或者DateTimeField字段, 字段名是birthday. 2. 数据表中已经有些数据 ...
- 2018 Multi-University Training Contest 4 Problem E. Matrix from Arrays 【打表+二维前缀和】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6336 Problem E. Matrix from Arrays Time Limit: 4000/20 ...
- Linux学习总结(十六)系统用户及用户组管理
先来认识两个文件 /etc/passwd/etc/shadow我们打印出首尾三行,来了解下:每行由:分割为7段,每段含义为:第一段:用户名,比如root 用户,普通用户test,lv,test1第二段 ...
- 解决mac 下mysql安装后root用户登录密码错误问题
使用的mac OS 10.11 安装mysql后访问root/root用户失败,网上找了一些解决办法,下面记录下解决方法方便以后自己查询 概述(看懂下面就不用看了): 停服务:sudo /usr/l ...
- 关于JWT.NET
1.JWT的概念: JWT全称是Json Web Token,是一种用于双方之间传递安全信息的简洁的.URL安全的表述性声明规范.JWT作为一个开放的标准( RFC 7519 ),定义了一种简洁的,自 ...
- Grunt中批量无损压缩图片插件--Grunt-contrib-imagemin
Photoshop 切出的图片,无论是 PNG 还是 JPEG/JPG 格式,都含有许多相关信息,又或多余的颜色值,这些信息和颜色值,对网页前端并没有用处,反而增加图片大小,所以 Google Pag ...
- 【题解】洛谷P1514 [NOIP2010TG] 引水入城(DFS+DP)
次元传送门:洛谷P1514 思路 可以证明如果有解 那么每个蓄水池可以覆盖到的干旱区必定是线段 证明: 举个栗子 8 9 8 7 9 7 6 9 6 明显到不了中间的点 如果不是连续的线段 中间肯定有 ...
- Spring入门第二课:Spring配置Bean的细节
1.配置bean的作用域: 通过配置scope属性可以bean的作用域,参数有 prototype.request.session.singleton. 1)singleton为单例,IoC容器只会创 ...
- Swift_协议
Swift_协议 点击查看源码 //协议 @objc protocol SomeProtocol:class { //class代表只用类才能实现这个协议 func test() //@objc:OC ...