iT邦幫忙

2021 iThome 鐵人賽

DAY 11
0
AI & Data

想到甚麼打甚麼系列 第 11

找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


上一篇
找LeetCode上簡單的題目來撐過30天啦(DAY10)
下一篇
找LeetCode上簡單的題目來撐過30天啦(DAY12)
系列文
想到甚麼打甚麼30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言