首页 > 其他分享 >36.有效的数独

36.有效的数独

时间:2022-08-16 09:00:33浏览次数:59  
标签:int 36 有效 board && 转载 数独

 

 

输入: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

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

 

class Solution {
    boolean judge1 (char[][] board,int x){
        int a[]={0,1,1,1,1,1,1,1,1,1};
        for(int i=0;i<9;++i){
            //判断是数字
            if(board[x][i]>='0'&&board[x][i]<='9'){
                int cnt =(int)board[x][i] - (int)'0';
                if(a[cnt]==1){
                    a[cnt]=0;
                }
                else {
                    return false;
                }
                //int cnt =board[x][i] - '0';
            }
        }
        return true;
    }
    boolean judge2 (char[][] board,int y){
        int a[]={0,1,1,1,1,1,1,1,1,1};
        for(int i=0;i<9;++i){
            //判断是数字
            if(board[i][y]>='0'&&board[i][y]<='9'){
                int cnt =(int)board[i][y] - (int)'0';
                if(a[cnt]==1){
                    a[cnt]=0;
                }
                else {
                    return false;
                }
                
            }
        }
        return true;
    }
    boolean judge3 (char[][] board,int x,int y){
        int a[]={0,1,1,1,1,1,1,1,1,1};
        for(int i=x;i<x+3;++i){
            for(int j=y;j<y+3;++j){
                if(board[i][j]>='0'&&board[i][j]<='9'){
                    int cnt = (int)board[i][j] - (int)'0';
                    if(a[cnt]==1){
                    a[cnt]=0;
                }
                else {
                    return false;
                }
                }
            }
        }
        return true;
    }
    public boolean isValidSudoku(char[][] board) {
        for(int i=0;i<9;++i){
            if(judge1(board,i)==false){
                return false;
            }
            if(judge2(board,i)==false){
                return false;
            }
        }
        for(int i=0;i<9;i+=3){
            for(int j=0;j<9;j+=3){
                if(judge3(board,i,j)==false){
                    return false;
                }
            }
        }
        return true;
    }
}

 

标签:int,36,有效,board,&&,转载,数独
From: https://www.cnblogs.com/h404nofound/p/16590368.html

相关文章

  • windows11上打开ie11浏览器(亲测有效)
    使用vbs脚本方式第一步:新建txt文件,复制以下代码CreateObject("InternetExplorer.Application").Visible=true保存,修改后缀名为.vbs双击文件,即可打开ie11第二步在edge......
  • leetcode636-函数的独占时间
    函数的独占时间栈使用栈记录调用过程。如果log为start,那么就加入堆栈。如果是end,则分两种情况:栈不为空,意味着存在递归调用,那么说明当前的函数占用了上一个函数的使用......
  • 36.特殊语句
    ......
  • UPC2022暑期个人训练赛第36场
    多谢两位大佬的帮助,才能勉强完成几个题,这几个题还是挺有意思的问题A:WJ的逃离DFS超时,所以考虑BFS,记得上次炸僵尸也是这个教训,这次忘记了感谢sgjen大佬提供的帮......
  • 《JavaStudy36》多态
     ......
  • MyBatisCodeHelperPro激活方法(有效方法)
    注意事项我的idea是2021.2.1的,新版本的idea我也不清楚。我这个版本是可以的。​编辑1、下载插件​编辑这是插件下载的地址:MybatisCodeHelperNew-Pro_免费高速下载|百......
  • 36
    virtually实际上         pronoun代词television电视机pamphlet小册子farther更远的bright明亮的regardless无论如何knife刀create创......
  • Qt开发经验小技巧236-240
    关于在头文件中定义函数使用static关键字的血的教训。有时候我们需要将一些常用函数写在一个文件中供很多地方调用,如果写的是intdoxxx{}这种,在你多个地方引用的时候......
  • HashMap-有效的数独
    问题描述:判断一个9x9的数独是否有效。只需要根据以下规则,验证已经填入的数字是否有效即可:数字1-9在每一行只能出现一次。数字1-9在每一列只能出现一次。数字......