首页 > 其他分享 >理解错题了 36. 有效的数独

理解错题了 36. 有效的数独

时间:2022-09-26 23:25:25浏览次数:58  
标签:数字 示例 36 错题 board 有效 数独

 理解错题了,就是让你在矩阵中循环判断而已,我还想到了康托付展开

请你判断一个 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)或者 '.'

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/valid-sudoku
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

/**
 * @param {character[][]} board
 * @return {boolean}
 */
var isValidSudoku = function(board) {
    // 理解错题了,就是让你在矩阵中循环判断而已
    const rectArr=[]
    for(let i=0;i<9;i++){
        rectArr.push([i,i+1,0,9])
        rectArr.push([0,9,i,i+1])
    }
    for(let i=0;i<9;i=i+3){
        for(let j=0;j<9;j=j+3){
            rectArr.push([i,i+3,j,j+3])
        }
    }
    console.log(rectArr)
    let isOk=true;
    for(let i=0;i<rectArr.length;i++){
        if(!isOk){
            break;
        }
        const arr=[]
        const rect=rectArr[i];
        for(let j=rect[0];j<rect[1];j++){
            for(let k=rect[2];k<rect[3];k++){
                const t=board[j][k];
                if(t!=='.'){
                    if(arr.indexOf(t)===-1){
                        arr.push(t)
                    }else{
                        isOk=false;
                        break
                    }
                }
            }
        }
    }

    return isOk
};

 

标签:数字,示例,36,错题,board,有效,数独
From: https://www.cnblogs.com/caoke/p/16732940.html

相关文章

  • 已知A矩阵为: 4 20 12 8 3 15 0 40 8 22 12 36 11 30 18 46
    已知A矩阵为: 4  20 12 8 3  15  0 40 8  22 12 36 11 30 18 46 将A中元素能被3整除的,全部置0  要求输出4200......
  • 已知A矩阵为: 4 20 12 8 3 15 0 40 8 22 12 36 11 30 18 46
    已知A矩阵为: 4  20 12 8 3  15  0 40 8  22 12 36 11 30 18 46 将A中元素大于10且小于25的数找出来,并输出该值在矩阵中的坐标......
  • 已知A矩阵为: 4 20 12 8 3 15 0 40 8 22 12 36 11 30 18 46 将A中
    已知A矩阵为:4  20 12 83  15  0 408  22 12 3611 30 18 46将A中元素小于等于12全部置0要求输出02000015040......
  • 36th 2022/8/11 模拟赛总结25
    这次真的不好,比赛一开始,揪着T1不放,当时脑子还短路,没想到讲循环一个个压回去,就是坐在那不停地想:斐波那契数列该乘什么……根本就像在发呆一样,足足发了一个钟,然后突然想到,可......
  • python中实现days360函数
    #-*-coding:utf-8-*-"""CreatedonFriSep2320:42:582022@author:Administrator"""importtimeimportdatetimedefdays360(start_date,end_date,meth......
  • Fedora 36 配置 zsh
    查看系统预设Shell。cat/etc/Shells安装zsh。sudodnf-yinstallzsh安装oh-my-zsh,使用一条命令即可。sh-c"$(curl-fsSLhttps://raw.github.com/ohmyzsh/oh......
  • [Oracle] LeetCode 1636 Sort Array by Increasing Frequency 双map
    Givenanarrayofintegersnums,sortthearrayinincreasingorderbasedonthefrequencyofthevalues.Ifmultiplevalueshavethesamefrequency,sortthem......
  • 算法与数据结构--有效数独
    classSolution{public:boolisValidSudoku(vector<vector<char>>&board){introws[9][9];//建造一个二维数组,记录行intcolumns[9][9];//建......
  • Linux错题集1
    错题1在Linux系统中,小王希望将他执行ls命令的输出结果保存在当前目录下的文件output.ls中,以供日后进行分析和使用,但要求不覆盖原文件的内容,他应该使用的命令是A.ls>outpu......
  • P7368 [USACO05NOV]Asteroids G
    题面贝茜想在\(N\timesN\)的网格中驾驶她的宇宙飞船。网格中有\(K\)个小行星。要使驾驶过程愉快,就必须把这些小行星全部消除。贝茜有一个武器,可以以一个单位代价消......