LeetCode-036-有效的数独
有效的数独
题目描述:请你判断一个 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-有效的数独的更多相关文章
- [LeetCode] 036. Valid Sudoku (Easy) (C++)
指数:[LeetCode] Leetcode 解决问题的指数 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 036. ...
- 前端与算法 leetcode 36. 有效的数独
目录 # 前端与算法 leetcode 36. 有效的数独 题目描述 概要 提示 解析 算法 传入[['5', '3', '.', '.', '7', '.', '.', '.', '.'],['6' ...
- [LeetCode] Sudoku Solver 求解数独
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- [LeetCode] Valid Sudoku 验证数独
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
- Java实现 LeetCode 36 有效的数独
36. 有效的数独 判断一个 9x9 的数独是否有效.只需要根据以下规则,验证已经填入的数字是否有效即可. 数字 1-9 在每一行只能出现一次. 数字 1-9 在每一列只能出现一次. 数字 1-9 在 ...
- leetcode 36 有效的数独 哈希表 unordered_set unordersd_map 保存状态 leetcode 37 解数独
leetcode 36 感觉就是遍历. 保存好状态,就是各行各列还有各分区divide的情况 用数组做. 空间小时间大 class Solution { public: bool isValidSud ...
- Java for LeetCode 036 Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
- [Leetcode] valid sudoku 有效数独
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
- LeetCode 36——有效的数独
1. 题目 2. 解答 将数独中数字的 ASCII 码值转化到 0-8 之间作为散列值,建立一个散列表,然后分别逐行.逐列.逐宫(3*3小块)统计每个数字的出现次数,若出现次数大于 1,则数独无效. ...
- [LeetCode] Sudoku Solver 解数独,递归,回溯
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
随机推荐
- window-server 服务器解决远程连接
mstsc 连接远程服务器,出现黑屏的情况: 解决方案: 运行 -> 输入gpedit.msc,进入组策略 -> 计算机配置 -> 管理模板 -> Windows组件 -> ...
- vue之Better-Scroll组件 将滚动条滚到最底部
首先我们需要使用scrollTo这个方法: scrollTo(x, y, time, easing) 参数: {Number} x 横轴坐标(单位 px) {Number} y 纵轴坐标(单位 px) ...
- VMware网络连接模式(桥接、NAT以及仅主机模式的详细介绍和区别)
VMware 桥接模式 VMware桥接模式,也就是将虚拟机的虚拟网络适配器与主机的物理网络适配器进行交接,虚拟机中的虚拟网络适配器可通过主机中的物理网络适配器直接访问到外部网络(例如图中所示的局域网 ...
- Eclipse集成Git/SVN插件及使用
感谢大佬:https://www.cnblogs.com/jpfss/p/8027347.html 1. Git插件安装 1.1 下载插件 首先打开Eclipse,然后点击Help>Instal ...
- JSP中的请求转发与重定向
在说请求转发和重定向之前,得了解下JSP九大内置对象中的response和request response:将服务器端数据发送到客户端,可通过在客户端浏览器中显示,用户浏览页面的重定向以及在客户端创建 ...
- 乐动ld06激光雷达sdk改bug记录分享
前言: 工作中,有使用过乐动ld06款激光雷达,此款雷达将常规雷达的转动的电机部分内置于自己的保护罩内,减少了雷达本身转动积灰等其他外界影响,探测半径是12m,是一款不错的雷达. 不过今天的主要内容不 ...
- zabbix 监控系统概述及部署
zabbix 监控系统概述及部署 1.Zabbix是什么: zabbix是一个个基于web界而的提供分布式系统监视以及网络监视功能的企业级的开源解决方案. zabbix能监视各种网络参数,保证服务器系 ...
- JSP页面 CTRL+F 功能实现
.res { color: rgba(255, 0, 0, 1) } .result { background: rgba(255, 255, 0, 1) } --- js 部分 var oldKey ...
- Redis 学习笔记(六)Redis 如何实现消息队列
一.消息队列 消息队列(Messeage Queue,MQ)是在分布式系统架构中常用的一种中间件技术,从字面表述看,是一个存储消息的队列,所以它一般用于给 MQ 中间的两个组件提供通信服务. 1.1 ...
- Express中使用session
1.安装express-session npm install express-session --save-dev //注意-g无效 2.app.jsvar session = require('e ...