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

网站建设中 模版游戏代理平台哪个好

网站建设中 模版,游戏代理平台哪个好,小程序模板教程,如何做文化传播公司网站文章目录36. 有效的数独:样例 1:样例 2:提示:分析:题解:rustgoccpythonjava36. 有效的数独: 请你判断一个 9 x 9 的数独是否有效。只需要 根据以下规则 ,验证已经填入的数字是否有效…

文章目录

  • 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.khdw.cn/news/40134.html

相关文章:

  • 什么网站可以买世界杯网站seo推广计划
  • wordpress 小说系统苏州seo关键词排名
  • 哪里有做标书seo综合查询软件排名
  • 国内自建的海淘网站搜索引擎查重
  • 怎么在58建设企业的网站风云榜小说排行榜
  • 北京朝阳网站建设百度推广开户渠道
  • 用nas 做网站seo技术公司
  • 杭州网站建设公司电话seo实战密码第四版
  • 手机怎么做网站卖东西搜索引擎优化seo什么意思
  • 企业手机网站建设方案seo和sem的联系
  • 网站404页面编写网址大全
  • 潍坊网站制作seo排名优化技巧
  • 哈尔滨建设网站公司哪家好网站seo优化有哪些方面
  • wordpress怎么制作网页seo自然排名优化
  • 网站建设咨询公全球网络营销公司排名
  • 专门做简历的网站有哪些qq刷赞网站推广快速
  • 网站备案要邮寄资料吗云搜索引擎
  • 廊坊北京网站建设今日国际新闻最新消息事件
  • 网站添加qq客服建网站找哪个平台好呢
  • 济南企业网站水果网络营销策划书
  • 智能建网站软件信息流广告哪个平台好
  • 聊城做网站的公司渠道一个新手怎么做电商
  • 网站建设意见新站点seo联系方式
  • 移动路由器做网站服务器seo专员工作内容
  • 企业为什么做网站素材推广网站制作
  • 网站开发的各个阶段及其完成的任务网络推广公司联系方式
  • 网站建设捌金手指下拉三推广搜索怎么选关键词
  • 网站访问权限百度公司招聘官网
  • 自助建站模板使用方法上海网站外包
  • 网站产品使用说明书怎么做网络营销师工作内容