当前位置: 首页 > news >正文

算法leetcode|36. 有效的数独(rust重拳出击)


文章目录

  • 36. 有效的数独:
    • 样例 1:
    • 样例 2:
    • 提示:
  • 分析:
  • 题解:
    • rust
    • go
    • c++
    • c
    • python
    • java


36. 有效的数独:

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

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

注意:

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

样例 1:

输入:board = [["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"]]输出:true

样例 2:

输入:board = [["8","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"]]输出:false解释:除了第一行的第一个数字从 5 改为 8 以外,空格内其他数字均与 示例1 相同。 但由于位于左上角的 3x3 宫内有两个 8 存在, 因此这个数独是无效的。

提示:

  • board.length == 9
  • board[i].length == 9
  • board[i][j] 是一位数字(1-9)或者 ‘.’

分析:

  • 面对这道算法题目,二当家的陷入了沉思。
  • 主要是如何存储行,列,以及3*3宫内出现过的值。
  • 方法很多,集合,整形数组,布尔数组都可以,只有1-9,一共9个数,最优化的空间方式应该是仅仅用一个整形,然后用位运算。

题解:

rust

impl Solution {pub fn is_valid_sudoku(board: Vec<Vec<char>>) -> bool {let mut rows = vec![vec![false; 9]; 9];let mut columns = vec![vec![false; 9]; 9];let mut sub_boxes = vec![vec![vec![false; 9]; 3]; 3];for i in 0..9 {for j in 0..9 {let c = board[i][j];if c != '.' {let index = (c as u8 - b'1') as usize;if rows[i][index] || columns[j][index] || sub_boxes[i / 3][j / 3][index] {return false;}rows[i][index] = true;columns[j][index] = true;sub_boxes[i / 3][j / 3][index] = true;}}}return true;}
}

go

func isValidSudoku(board [][]byte) bool {var rows, columns [9][9]boolvar subBoxes [3][3][9]boolfor i, row := range board {for j, c := range row {if c != '.' {index := c - '1'if rows[i][index] || columns[j][index] || subBoxes[i/3][j/3][index] {return false}rows[i][index] = truecolumns[j][index] = truesubBoxes[i/3][j/3][index] = true}}}return true
}

c++

class Solution {
public:bool isValidSudoku(vector<vector<char>>& board) {bool rows[9][9];bool columns[9][9];bool subBoxes[3][3][9];memset(rows, 0, sizeof(rows));memset(columns, 0, sizeof(columns));memset(subBoxes, 0, sizeof(subBoxes));for (int i = 0; i < 9; i++) {for (int j = 0; j < 9; j++) {char c = board[i][j];if (c != '.') {int index = c - '1';if (rows[i][index] || columns[j][index] || subBoxes[i / 3][j / 3][index]) {return false;}rows[i][index] = true;columns[j][index] = true;subBoxes[i / 3][j / 3][index] = true;}}}return true;}
};

c

bool isValidSudoku(char** board, int boardSize, int* boardColSize){bool rows[9][9];bool columns[9][9];bool subBoxes[3][3][9];memset(rows, 0, sizeof(rows));memset(columns, 0, sizeof(columns));memset(subBoxes, 0, sizeof(subBoxes));for (int i = 0; i < 9; i++) {for (int j = 0; j < 9; j++) {char c = board[i][j];if (c != '.') {int index = c - '1';if (rows[i][index] || columns[j][index] || subBoxes[i / 3][j / 3][index]) {return false;}rows[i][index] = true;columns[j][index] = true;subBoxes[i / 3][j / 3][index] = true;}}}return true;
}

python

class Solution:def isValidSudoku(self, board: List[List[str]]) -> bool:rows, columns, sub_boxes = [[False] * 9 for _ in range(9)], [[False] * 9 for _ in range(9)], [[[False] * 9 for _ in range(3)] for _ in range(3)]for i in range(9):for j in range(9):c = board[i][j]if c != '.':index = ord(c) - ord('1')if rows[i][index] or columns[j][index] or sub_boxes[i // 3][j // 3][index]:return Falserows[i][index] = Truecolumns[j][index] = Truesub_boxes[i // 3][j // 3][index] = Truereturn True

java

class Solution {public boolean isValidSudoku(char[][] board) {boolean[][]   rows     = new boolean[9][9];boolean[][]   columns  = new boolean[9][9];boolean[][][] subBoxes = new boolean[3][3][9];for (int i = 0; i < 9; i++) {for (int j = 0; j < 9; j++) {char c = board[i][j];if (c != '.') {int index = c - '1';if (rows[i][index] || columns[j][index] || subBoxes[i / 3][j / 3][index]) {return false;}rows[i][index] = true;columns[j][index] = true;subBoxes[i / 3][j / 3][index] = true;}}}return true;}
}

非常感谢你阅读本文~
欢迎【点赞】【收藏】【评论】~
放弃不难,但坚持一定很酷~
希望我们大家都能每天进步一点点~
本文由 二当家的白帽子:https://le-yi.blog.csdn.net/ 博客原创~


http://www.lryc.cn/news/5029.html

相关文章:

  • 都说爱有回音,这次情人节驱动人生宠你!
  • npm安装依赖报错 npm code ERESOLVEnpm ERESOLVE unable to resolve dependency tree
  • 【项目设计】—— 基于Boost库的搜索引擎
  • 全网详解MyBatis-Plus updateById方法更新不了空字符串或null的解决方法
  • 联想K14电脑开机全屏变成绿色无法使用怎么U盘重装系统?
  • Spring Boot HTTP 400排查
  • 【手撕源码】vue2.x中keep-alive源码解析
  • ROS2机器人编程简述humble-第四章-BASIC DETECTOR .3
  • 【图像分类】基于PyTorch搭建LSTM实现MNIST手写数字体识别(双向LSTM,附完整代码和数据集)
  • 【Linux】多线程编程 - 同步/条件变量/信号量
  • ES优化方案
  • 从数据备份保护到完整生命周期管理平台,爱数全新发布 AnyBackup Family 8
  • Go 微服务开发框架 DMicro 的设计思路
  • 浅谈功能测试
  • UDP的详细解析
  • 史上最详细JUC教程之Synchronized与锁升级详解
  • Vue|初识Vue
  • 在职阿里6年,一个29岁女软件测试工程师的心声
  • (C语言)自定义类型,枚举与联合
  • node.js服务端笔记文档学会写接口,学习分类:path、包、模块化、fs、express、中间件、jwt、开发模式、cors。
  • 初始C++(三):引用
  • 【前端】参考C站动态发红包界面,高度还原布局和交互
  • VR全景带你浪漫“狂飙”情人节,见证甜蜜心动
  • Linux系统安全之iptables防火墙
  • 【C#基础】C# 变量与常量的使用
  • [ 常用工具篇 ] CobaltStrike(CS神器)基础(一) -- 安装及设置监听器详解
  • Redis集群
  • 00---C++入门
  • Spring-事务2
  • Windows Git Bash 配置