有效的数独

题目描述:请你判断一个 9x9 的数独是否有效。只需要 根据以下规则 ,验证已经填入的数字是否有效即可。

数字 1-9 在每一行只能出现一次。

数字 1-9 在每一列只能出现一次。

数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。(请参考示例图)

数独部分空格内已填入了数字,空白格用 '.' 表示。

注意:

  • 一个有效的数独(部分已被填充)不一定是可解的。
  • 只需要根据以上规则,验证已经填入的数字是否有效即可。

示例说明请见LeetCode官网。

来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/valid-sudoku/

著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

解法一:数组遍历

分为3种情况判断,分别是行判断、列判断、3*3宫内判断,判断逻辑是利用Set判重,如果在同一行(或同一列、同一宫内)有重复的数字,则返回false;如果都符合,最后返回true。

import javafx.util.Pair;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set; public class LeetCode_036 {
public static List<Pair<Integer, Integer>> all = new ArrayList<>(); static {
all.add(new Pair<>(0, 0));
all.add(new Pair<>(0, 3));
all.add(new Pair<>(0, 6));
all.add(new Pair<>(3, 0));
all.add(new Pair<>(3, 3));
all.add(new Pair<>(3, 6));
all.add(new Pair<>(6, 0));
all.add(new Pair<>(6, 3));
all.add(new Pair<>(6, 6));
} public static boolean isValidSudoku(char[][] board) {
// 行判断
for (int i = 0; i < board.length; i++) {
Set<Character> nums = new HashSet<>();
for (int j = 0; j < board[i].length; j++) {
if (board[i][j] != '.' && !nums.add(board[i][j])) {
return false;
}
}
}
// 列判断
for (int i = 0; i < board.length; i++) {
Set<Character> nums = new HashSet<>();
for (int j = 0; j < board.length; j++) {
if (board[j][i] != '.' && !nums.add(board[j][i])) {
return false;
}
}
}
// 宫内判断
for (Pair<Integer, Integer> integerIntegerPair : all) {
Set<Character> nums = new HashSet<>();
for (int x = integerIntegerPair.getKey(); x < integerIntegerPair.getKey() + 3; x++) {
for (int y = integerIntegerPair.getValue(); y < integerIntegerPair.getValue() + 3; y++) {
if (board[x][y] != '.' && !nums.add(board[x][y])) {
return false;
}
}
}
} return true;
} public static void main(String[] args) {
char[][] board = new char[][]{{'5', '3', '.', '.', '7', '.', '.', '.', '.'}
, {'6', '.', '.', '1', '9', '5', '.', '.', '.'}
, {'.', '9', '8', '.', '.', '.', '.', '6', '.'}
, {'8', '.', '.', '.', '6', '.', '.', '.', '3'}
, {'4', '.', '.', '8', '.', '3', '.', '.', '1'}
, {'7', '.', '.', '.', '2', '.', '.', '.', '6'}
, {'.', '6', '.', '.', '.', '.', '2', '8', '.'}
, {'.', '.', '.', '4', '1', '9', '.', '.', '5'}
, {'.', '.', '.', '.', '8', '.', '.', '7', '9'}};
System.out.println(isValidSudoku(board));
}
}

【每日寄语】 希望生活给予你风浪的同时,也给你阳光作为回报,让你感受到这个世界的温柔。

LeetCode-036-有效的数独的更多相关文章

  1. [LeetCode] 036. Valid Sudoku (Easy) (C++)

    指数:[LeetCode] Leetcode 解决问题的指数 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 036. ...

  2. 前端与算法 leetcode 36. 有效的数独

    目录 # 前端与算法 leetcode 36. 有效的数独 题目描述 概要 提示 解析 算法 传入[['5', '3', '.', '.', '7', '.', '.', '.', '.'],['6' ...

  3. [LeetCode] Sudoku Solver 求解数独

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  4. [LeetCode] Valid Sudoku 验证数独

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...

  5. Java实现 LeetCode 36 有效的数独

    36. 有效的数独 判断一个 9x9 的数独是否有效.只需要根据以下规则,验证已经填入的数字是否有效即可. 数字 1-9 在每一行只能出现一次. 数字 1-9 在每一列只能出现一次. 数字 1-9 在 ...

  6. leetcode 36 有效的数独 哈希表 unordered_set unordersd_map 保存状态 leetcode 37 解数独

    leetcode 36 感觉就是遍历. 保存好状态,就是各行各列还有各分区divide的情况 用数组做. 空间小时间大 class Solution { public: bool isValidSud ...

  7. Java for LeetCode 036 Valid Sudoku

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...

  8. [Leetcode] valid sudoku 有效数独

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...

  9. LeetCode 36——有效的数独

    1. 题目 2. 解答 将数独中数字的 ASCII 码值转化到 0-8 之间作为散列值,建立一个散列表,然后分别逐行.逐列.逐宫(3*3小块)统计每个数字的出现次数,若出现次数大于 1,则数独无效. ...

  10. [LeetCode] Sudoku Solver 解数独,递归,回溯

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

随机推荐

  1. SNAT技术

    前面在讲解 firewall-config 工具的功能时,曾经提到了 SNAT(Source Network Address Translation,源网络地址转换)技术.SNAT 是一种为了解决 I ...

  2. 使用XmlWriter写入XML

    麻了..整理完了发现XmlWriter不能添加元素,只能重写,还是得用Xdocument..好像DOM能实现添加元素 点击查看代码 **MemoryStream msXml = new MemoryS ...

  3. SharePoint Online 为Modern Page添加脚本

    前言 众所周知,如果我们想向SharePoint 页面添加脚本,最方便的便是经典页面,添加方式主要有内容编辑器Web部件或者直接使用SharePoint Designer. 但是,如果页面是Moder ...

  4. StringUtils.isBlank(str)和StringUtils.isEmpty(str)的区别

    1.StringUtils.isEmpty(CharSequence cs)实现源码 public static boolean isEmpty(CharSequence cs) { return c ...

  5. position和anchorPoint笔记

    position和anchorPoint是CAlayer的两个属性.     我们以前修改一个控件的位置都是能过Frame的方式进行修改.     现在利用CALayer的position和ancho ...

  6. Linux防火墙firewalld安全设置

    背景描述 防火墙是具有很好的保护作用.入侵者必须首先穿越防火墙的安全防线,才能接触目标计算机.在公司里数据安全是最重要的,要求安全部门进行全公司进行服务器防火墙安全搭建,在原有的基础上进行安全的防火墙 ...

  7. Java向mysql中插入时间的方法

    ava向MySQL插入当前时间的四种方式和java时间日期格式化的几种方法(案例说明);部分资料参考网络资源  java向MySQL插入当前时间的四种方式 第一种:将java.util.Date类型的 ...

  8. 推荐一款仿iPhone桌面的代码. ___王朋.

    Demo:https://files.cnblogs.com/files/sixindev/LxGridView-master.zip 这是作者原来的效果图,很多东西还需要慢慢学习.作者用的很多类,根 ...

  9. Linux常用命令整理:文件目录管理

    据说,你要对Linux文件做的事情,98%都记录在这篇文章里了. 1.ls命令 最常见的命令,相信刚进入linux命令行界面的时候,都要用这个命令看看当前目录下都有哪些文件吧. 名称:List 含义: ...

  10. http的报文结构和状态码总结

    1.HTTP报文结构 请求报文 报文由三个部分组成,即开始行.首部行和实体主体.在请求报文中,开始行就是请求行. 响应报文 响应报文的开始行是状态行.状态行包括三项内容,即HTTP的版本,状态码,以及 ...