有效的数独

题目描述:请你判断一个 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. Kubernetes之日志和监控(十五)

    一.日志和监控 1.1.Log 1.1.1.容器级别 通过docker命令查看容器级别的日志 docker ps --->containerid docker logs containerid ...

  2. linux apache软件安装

    安装提示 Linux下,源码的安装一般由3个步骤组成:配置(configure).编译(make).安装(make install). 过程中用到"configure --prefix=安装 ...

  3. CNN-卷积神经网络简单入门(2)

    在上篇中,对卷积神经网络的卷积层以及池化层模块进行了简单的介绍,接下来将对卷积神经网络的整个运作流程进行分析,以便对CNN有个总体上的认知和掌握. 如下图,卷积神经网络要完成对图片数字的识别任务.网络 ...

  4. Android下数据库创建

    什么情况下我们才用数据库做数据存储? 大量数据结构相同的数据需要存储时. sqlite 嵌入式 轻量级 SqliteOpenHelper 创建数据库步骤: 1.创建一个类集成SqliteOpenHel ...

  5. js实现网页中英文翻译

    1,html 2,metrics.js 3,需要 http://www.microsoftTranslator.com/ajax/v3/WidgetV3.ashx?siteData=ueOIGRSKk ...

  6. IP地址与子网划分

    IP地址与子网划分 目录 IP地址与子网划分 一.IP地址(Internet Protocol Address) 1.IP地址的表示 2.IP地址的组成 3.IP地址的分类 (1)A类IP地址 (2) ...

  7. Oracle 撤回已经提交的事务

    在PL/SQL操作了一条delete语句习惯性的commit 了,因少加了where条件 导致多删了数据 1.查询视图v$sqlarea,找到操作那条SQL的时间(FIRST_LOAD_TIME) s ...

  8. VNCTF RE复现 (BabyMaze 时空飞行)

    babymaze pyc混淆! 还没反编译出来 只能找个脚本偷字节码 import marshal, dis f = open('babymaze.pyc', 'rb') f.read(4) f.re ...

  9. 从 MMU 看内存管理

    在计算机早期的时候,计算机是无法将大于内存大小的应用装入内存的,因为计算机读写应用数据是直接通过总线来对内存进行直接操作的,对于写操作来说,计算机会直接将地址写入内存:对于读操作来说,计算机会直接读取 ...

  10. 磁盘分区 & Linux 三剑客之 awk

    今日内容 磁盘分区 Linux 三剑客之 awk 内容详细 一.磁盘分区 磁盘分区 --> 挂载 步骤 1.关机 2.添加硬盘 3.创建分区 fdisk /dev/sdb or gdisk /d ...