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. POJ-1845 Sumdiv---因子和(快速幂+快速加法+因子和公式)

    题目链接: https://cn.vjudge.net/problem/POJ-1845 题目大意: 求AB的因子和 解题思路: 先将A质因数分解,然后B次方的质因数指数就是乘上B即可 这里要mod9 ...

  2. 使用 JDK XML 和 java对象相互转换

    Unmarshaller 类能将 XML 数据转换为 Java 内容对象. Marshaller 类能够将 Java 对象转换回 XML 数据. package jaxb; /** * Created ...

  3. Oracle表空间、段、区和块简述

    本文转载自:http://blog.itpub.net/17203031/viewspace-682003/ 在Oracle学习过程中,存储结构,表段区块可能是每个初学者都要涉及到的概念.表空间.段. ...

  4. 【转】九步学习python装饰器

    本篇日志来自:http://www.cnblogs.com/rhcad/archive/2011/12/21/2295507.html 纯转,只字未改.只是为了学习一下装饰器.其实现在也是没有太看明白 ...

  5. Linux_脚本——使用echo从一个文件写入还有一个文件末尾

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/svitter/article/details/30980923 echo $(cat 你须要的文件) ...

  6. Linux 学习总结(五)-linux 文件系统及相关命令

    一 linux文件系统概要 linux系统结构有别用于windos,他是树状结构的文件系统,在linux下我们称一切皆文件,我们将一个目录,可以成称为目录文件.linux只有一个单独的顶级目录结构.所 ...

  7. 根据用户id生成一个唯一邀请码

    需求描述:根据用户id生成与之对应的唯一邀请码,范围为‘0-9A-Z’. 这个需求的重点在于加粗的部分,也就是要能够根据邀请码反推出用户ID,这样邀请码就不用入库了,在用户量很大的情况下,性能可以得到 ...

  8. 21、整合Druid数据源

    1).引入外部的数据源(Druid) <!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependenc ...

  9. 20145223 杨梦云 《网络对抗》 Web安全基础实践

    20145223 杨梦云 <网络对抗> Web安全基础实践 1.实验后回答问题 (1)SQL注入攻击原理,如何防御 **原理**:SQL注入攻击是通过构建特殊的输入作为参数传入web应用程 ...

  10. Unity中自定义扩展方法

    问题背景 在使用unity开发过程中,通常会遇到一种情况,比如说给物体重新赋值坐标的问题, Transfrom tran: ,pos_y=,pos_z=; tran.position=new Vect ...