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 ...
随机推荐
- SpringBoot使用异步线程池实现生产环境批量数据推送
前言 SpringBoot使用异步线程池: 1.编写线程池配置类,自定义一个线程池: 2.定义一个异步服务: 3.使用@Async注解指向定义的线程池: 这里以我工作中使用过的一个案例来做描述,我所在 ...
- python 小兵(4)之文件操作 小问题
1.光标不对就用seek 2.文件操作方面注意不要变修改变删除,会爆出文件正在运行不能操作 3.w模式下只有开始打开的时候会清空 4.文件操作的时候用as 后面的参数进行操作,不能用文件名进行操作 5 ...
- 「SNOI2017」一个简单的询问
「SNOI2017」一个简单的询问 简单的解法 显然可以差分一下. \[get(l,r,x)\times get(l1,r1,x)=get(1,r,x) \times get(1,r1,x)-get( ...
- Argo 安装和 workflow 实例配置文件解析
一.Argo 安装配置 1.1 Argo 安装 $ kubectl create ns argo $ kubectl apply -n argo -f https://raw.githubuserco ...
- Redis集群安装详细步骤
环境: Centos7 redis3.0 三台虚拟机主机名分别为 master node1 node2 如果单机的时候设置过密码最好把密码去掉,避免位置的错误. 拍个快照方便恢复. 1.创 ...
- html5 新增标签和特性
文档类型设定 document HTML: XHTML: HTML5 字符设定 <meta http-equiv="charset" content="utf-8& ...
- dealloc方法
dealloc方法基本概念 当一个对象的引用计数器值为0时,这个对象即将被销毁,其占用的内存被系统回收 对象即将被销毁时系统会自动给对象发送一条dealloc消息 (因此, 从dealloc方法有没有 ...
- MacOS常用命令行工具
转自:https://blog.csdn.net/u014102846/article/details/77964493 https://ohmyz.sh/ https://github.com/ro ...
- 36、python并发编程之多线程(操作篇)
目录: 一 threading模块介绍 二 开启线程的两种方式 三 在一个进程下开启多个线程与在一个进程下开启多个子进程的区别 四 练习 五 线程相关的其他方法 六 守护线程 七 Python GIL ...
- Solution -「多校联训」最大面积
\(\mathcal{Description}\) Link. 平面上有 \(n\) 个点 \(A_{1..n}\),\(q\) 次询问,每次给出点 \(P\),求 \[\max_{1\le ...