找LeetCode上简单的题目来撑过30天啦(DAY11)

今天好累,直接上题目

题号:36 标题:Valid Sudoku 难度:Medium

Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:

Each row must contain the digits 1-9 without repetition.
Each column must contain the digits 1-9 without repetition.
Each of the nine 3 x 3 sub-boxes of the grid must contain the digits 1-9 without repetition.

Note:
A Sudoku board (partially filled) could be valid but is not necessarily solvable.
Only the filled cells need to be validated according to the mentioned rules.

Input: 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"]]
Output: true

Example 2:
Input: 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"]]
Output: false

Explanation: Same as Example 1, except with the 5 in the top left corner being modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid.

Constraints:
board.length == 9
board[i].length == 9
board[i][j] is a digit 1-9 or '.'.


我的程序码

bool isValidSudoku(char** board, int boardSize, int* boardColSize){
    int check[9] = {1,1,1,1,1,1,1,1,1};
    int i=0,j=0;
    for(i=0;i<9;i++){
        for(j=0;j<9;j++){
            if(board[i][j] == '.'){
                continue;
            }else if(check[(board[i][j]-49)] == 0){
                    printf("first %d,%d",i,j);
                    return false;
            }else{
               check[board[i][j]-49] = 0; 
            }
        }
        for(j=0;j<9;j++){
           check[j] = 1; 
        } 
    }
    
    for(j=0;j<9;j++){
        for(i=0;i<9;i++){
            if(board[i][j] == '.'){
                continue;
            }else if(check[board[i][j]-49] == 0){
                printf("second %d,%d",i,j);    
                return false;
            }else{
               check[board[i][j]-49] = 0; 
            }
        }
        for(i=0;i<9;i++){
           check[i] = 1; 
        } 
    }
    
  
    int a,b;
    
    for(a=0;a<=6;a=a+3){
        for(b=0;b<=6;b=b+3){
            for(i=a;i<a+3;i++){
                for(j=b;j<b+3;j++){
                    if(board[i][j] == '.'){
                        continue;
                    }else if(check[board[i][j]-49] == 0){
                        printf("#3 %d,%d",i,j);
                        return false;
                    }else{
                         check[board[i][j]-49] = 0; 
                    }
                }
            }
            
            for(i=0;i<9;i++){
                check[i] = 1;
            }
        }
    }

DAY11心得
这题很快,我好累,明年的我大概也会想不开来参赛,但要上班还要拼命30天,真的好痛苦阿/images/emoticon/emoticon03.gif


<<:  #11 Pandas教学3

>>:  Day 14【连动 MetaMask - Front-End Request and Fetch】Modern problems require modern solutions

【Day 5】VSCode移动GIT里的HEAD

何谓HEAD? 说明 : 表示目前指向的档案版本指标,通常是指向最新的commit档案。 可以先透过...

为了转生而点技能-JavaScript,day26(Event初探

Event(事件):当满足触发事件的条件时,会触发设定好的事件,并执行触发後的动作。 目标.addE...

Day 05 LINE bot上的Webhooks

Webhooks介绍 Webhooks在LINE bot里面做什麽 如前面提到Messaging A...

Day40. 范例:假期规划 (建造者模式)

本文同步更新於blog 情境:目前提供旅游行程的方式 <?php namespace Ap...

D3JsDay05Bar拉BarBarBar,作伙来画吧—画个bar chart长条图

用D3绘制长条图 我们现在可以尝试着用已经学到的SVG来画长条图,只不过是透过D3Js的操作来新增S...