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)的更多相关文章

  1. LeetCode:39. Combination Sum(Medium)

    1. 原题链接 https://leetcode.com/problems/combination-sum/description/ 2. 题目要求 给定一个整型数组candidates[ ]和目标值 ...

  2. LeetCode:43. Multiply Strings (Medium)

    1. 原题链接 https://leetcode.com/problems/multiply-strings/description/ 2. 题目要求 给定两个String类型的正整数num1.num ...

  3. 【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 ...

  4. LeetCode:16. 3Sum Closest(Medium)

    1. 原题链接 https://leetcode.com/problems/3sum-closest/description/ 2. 题目要求 数组S = nums[n]包含n个整数,找出S中三个整数 ...

  5. LeetCode:49. Group Anagrams(Medium)

    1. 原题链接 https://leetcode.com/problems/group-anagrams/description/ 2. 题目要求 给定一个字符串数组,将数组中包含相同字母的元素放在同 ...

  6. LeetCode:22. Generate Parentheses(Medium)

    1. 原题链接 https://leetcode.com/problems/generate-parentheses/description/ 2. 题目要求 给出一个正整数n,请求出由n对合法的圆括 ...

  7. LeetCode:20. Valid Parentheses(Easy)

    1. 原题链接 https://leetcode.com/problems/valid-parentheses/description/ 2. 题目要求 给定一个字符串s,s只包含'(', ')',  ...

  8. LeetCode:9. Palindromic Number(Medium)

    原题链接:https://leetcode.com/problems/palindrome-number/description/ 1. 题目要求:判断一个int类型整数是否是回文,空间复杂度O(1) ...

  9. 【LeetCode】36. Valid Sudoku 解题报告(Python)

    [LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...

随机推荐

  1. hdu 6169 gems gems gems【DP】

    题目链接:hdu 6169 gems gems gems Now there are n gems, each of which has its own value. Alice and Bob pl ...

  2. 我上线的android版app

    把自己开发的几个小的app上线了,在自己的博客中推广一下吧: 聊天兔子: 下载地址:http://android.myapp.com/myapp/detail.htm?apkName=com.fuly ...

  3. Oracle创建自增序列

    Oracle没有自增字段这样的功能,但是通过触发器(trigger)和序列(sequence)可以实现. 先建一个测试表了: create table userlogin( id   number(6 ...

  4. SOJ4459 skysky's game(贪心+优先队列)

    天天最近迷上了天天爱消除游戏,现在他觉得这个游戏已经没有意思了.所以他发明一个新的消除游戏.有n堆糖果,每一个糖果有一个重量w,天天每次都选择两个糖果合并为一个糖果,新的糖果的重量等于这两个糖果的重量 ...

  5. AWESOME SWIFT-swift.libhunt.com-swift类库网站

    https://swift.libhunt.com/categories/688-events 29 Events libraries and projects ORDERED BY POPULARI ...

  6. 「CF375D Tree and Queries」

    题目 \(dsu\ on\ tree\)的板子题了 \(dsu\ on\ tree\)本质上一种优秀通过轻重链剖分优化到\(O(nlogn)\)的暴力 一般用来解决没有修改的允许离线的子树查询问题 首 ...

  7. how to design Programs 学习笔记

    how to design Programs 学习笔记 */--> how to design Programs 学习笔记 目录 1. 前言 1.1. 系统化程序设计 1.2. 输入和输出 2. ...

  8. 我的第一个C++程序

    准备抽空学习C++了,不知道自己以后能不能坚持下去,去百度查了一下入门,大多数朋友都是选择用VC++或者VS,而我这里用的是C-Free 5 ,安装包也只有十几兆. 用起来也方便.对于初学者而言够用了 ...

  9. stack的三个意思

    (转自阮一峰的网络日志,原网址http://www.ruanyifeng.com/blog/2013/11/stack.html) 阮一峰老师终于又更新博客了,个人认为这篇文章有一定科普意义,有一定解 ...

  10. Many-to-many relationships in EF Core 2.0 – Part 3: Hiding as ICollection

    In the previous post we ended up with entities that hide the join entity from the public surface. Ho ...