有效的数独

题目描述:请你判断一个 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. C++/WinUI 3 技术笔记(一)

    微软在 Windows 10 Version 1809 上正式发布了新的 UI 框架,命名为 WinUI 3. 这已经是微软发布的第不知道多少个 UI 框架了,但是微软宣称它将支持原生 C++ 和 W ...

  2. open policy agent 语法总结

    OPA 文档模型 OPA将从外部加载的数据成为基本文档(base documents),有规则产生的值成为虚拟文档(virtual documents),此处"虚拟"的意思表示文档 ...

  3. document对象常用属性

    转载请注明来源:https://www.cnblogs.com/hookjc/ 注:页面上元素name属性和JavaScript引用的名称必须一致包括大小写    否则会提示你一个错误信息 " ...

  4. Kubernetes二进制(单/多节点)部署

    Kubernetes二进制(单/多节点)部署 目录 Kubernetes二进制(单/多节点)部署 一.常见的K8S部署方式 1. Minikube 2. Kubeadmin 3. 二进制安装部署 4. ...

  5. 分享刚出炉的基于Blazor技术的Web应用开发框架

    这是最近刚刚重构完成的项目,有点迫不及待的分享给大家,为了跟上技术升级把原来基于MVC Razor Page开源项目 RazorPageCleanArchitecture 进行重构, 前端用Blazo ...

  6. 同事提出个我从未想过的问题,为什么Kubernetes要"多此一举"推出静态Pod概念?

    同事提出个我从未想过的问题,为什么Kubernetes要"多此一举"推出静态Pod概念? 我们知道k8s中Pod可以说是一个合格的容器小管家,Pod 被设计成支持多个容器可以一起进 ...

  7. 羽夏闲谈—— C 语言入门之问

    前言   最近加入了一个QQ频道,有很多想学C的纯小白.为什么这么说呢?因为他们会问一些环境用啥?为啥我配置不行?看了教程配置环境我也不会,咋配置?为啥这里代码这里有错误?啥语言好不好?诸如此类的问题 ...

  8. Vue 源码解读(1)—— 前言

    当学习成为了习惯,知识也就变成了常识. 感谢各位的 点赞.收藏和评论. 新视频和文章会第一时间在微信公众号发送,欢迎关注:李永宁lyn 文章已收录到 github 仓库 liyongning/blog ...

  9. 零基础自学Python十天的时候,写的一款猜数字小游戏,附源码和软件下载链接!

    自学一门语言最重要的是要及时给自己反馈,那么经常写一些小程序培养语感很重要,写完可以总结一下程序中运用到了哪些零散的知识点. 本程序中运用到的知识点有: 1.输入输出函数 (input.print) ...

  10. SpringBoot2.x设置文件上传文件的大小

    The field file exceeds its maximum permitted size of 1048576 bytes spring: # 设置文件上传文件大小 servlet: mul ...