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

36. 有效的数独

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

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

注意:

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

C++

class Solution {
public:
    bool isValidSudoku(vector<vector<char>>& board) {
        int rows[9][9];
        int cloumns[9][9];
        int subboxes[3][3][9];

        memset(rows,0,sizeof(rows));
        memset(cloumns,0,sizeof(cloumns));
        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-'0'-1;
                    //printf("c:%c|c-1:%d|c-0:%d.",c,c-1,c-'0');
                    //printf("c-0-1:%d.",c-'0'-1);
                    rows[i][index]++;
                    cloumns[j][index]++;
                    subboxes[i/3][j/3][index]++;
                    if( rows[i][index]>1 || cloumns[j][index]>1 || subboxes[i/3][j/3][index]>1 ){
                        return false;
                    }
                }
            }
        }
        return true;
    }
};

时间复杂度

O ( N 2 ) O(N^2) O(N2)

空间复杂度

O ( 1 ) O(1) O(1)

Java

class Solution {
    public boolean isValidSudoku(char[][] board) {
        int [][] rows=new int[9][9];
        int [][] columns=new int[9][9];
        int [][][] subboxes=new int [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=(int)(c-'0')-1;
                    rows[i][index]++;
                    columns[j][index]++;
                    subboxes[i/3][j/3][index]++;
                    if( rows[i][index]>1 || columns[j][index]>1 || subboxes[i/3][j/3][index]>1 ){
                        return false;
                    }
                }
            }
        }

        return true;
        
    }
}

时间复杂度

O ( N 2 ) O(N^2) O(N2)

空间复杂度

O ( 1 ) O(1) O(1)

Python

import numpy as np
class Solution:
    def isValidSudoku(self, board: List[List[str]]) -> bool:
        rows=np.zeros((9,9));
        columns=np.zeros((9,9));
        subboxes=np.zeros((3,3,9));
        for i in range(9):
            for j in range(9):
                c=board[i][j];
                if '.'!=c:
                    index=ord(c)-ord('0')-1;
                    rows[i,index]=rows[i,index]+1;
                    columns[j,index]=columns[j,index]+1;
                    subboxes[int(i/3),int(j/3),index]=subboxes[int(i/3),int(j/3),index]+1;
                    if rows[i][index]>1 or columns[j][index]>1 or subboxes[int(i/3)][int(j/3)][index]>1:
                        return False;
        return True;
        

时间复杂度

O ( N 2 ) O(N^2) O(N2)

空间复杂度

O ( 1 ) O(1) O(1)

相关文章:

  • 【deepseek】本地部署+webui访问
  • 论文阅读笔记:Continual Forgetting for Pre-trained Vision Models
  • Imagination GPU 3D Graphics Wrokload
  • linux基础语法2
  • 开源文件存储系统
  • 提升数据洞察力:五款报表软件助力企业智能决策
  • 【Mysql】我在广州学Mysql 系列—— 性能优化相关例题
  • 设计模式之代理设计模式
  • nodejs用ws模块反向代理socks5数据,即用ws写服务端和客户端及加密
  • Spring Boot + Apache POI 实现 Excel 导出:BOM物料清单生成器(支持中文文件名、样式美化、数据合并)
  • 2.26 notes on cxl
  • AI工具库:技术爱好者装备新势能
  • debian 12安装 postgresql 17
  • Idea java项目结构介绍
  • 使用插件 `vue2-water-marker`添加全局水印
  • 拐弯的时间线
  • 通过返回的key值匹配字典中的value值
  • C++ Primer Plus第八章课后习题总结
  • win11系统通过WSL安装ubuntu
  • 模电学习笔记
  • 医院网站建设的目标/sem与seo
  • html 网站建设中/百度竞价广告代理
  • 介绍一个公司的ppt/seo日常工作都做什么的
  • 做网站笔记本2014/广东又出现新病毒
  • b2c网站功能/seo外包资讯
  • 九天利建公司简介/seo快排技术教程