본문 바로가기
LeetCode

LeetCode Valid Sudoku

by 콩순이냉장고 2023. 6. 16.

문제 URL : https://leetcode.com/problems/valid-sudoku/description/

 

Valid Sudoku - LeetCode

Can you solve this real interview question? Valid Sudoku - Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules: 1. Each row must contain the digits 1-9 without repetition. 2. Each c

leetcode.com

 

문제접근법 :

 

스도쿠 문제입니다. 미완성 스도쿠이지만 

주어진 숫자가 주어질때 완성되게 만들수있는지 없는지 판단하는 문제입니다.

매우간단한게

행에 1~9까지 중복으로 채워져있는지

열에도 마찬가지로 1~9로 채워져있는지

3x3 집합군으로 따지면 1~9로 채워져있는지

집합군은 (i/3)*3 + j/3 을 이용하면  3x3 집합마다 0~8까지 숫자로 볼수있습니다.

 

소스코드 : 

 

#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
    bool isValidSudoku(vector<vector<char>>& board) {
        int column[10][10]={0};
        int row[10][10]={0};
        int sets[10][10]={0};
        for(int i=0;i<9;i++){
            for(int j=0;j<9;j++){
                if(board[i][j]=='.')continue;
                
                int num= board[i][j]-'0';
                if(row[i][num]||column[j][num]||sets[(i/3)*3+j/3][num])return false;
                row[i][num]=1;
                column[j][num]=1;
                sets[(i/3)*3+j/3][num]=1;
            }
        }
        return true;
    }

};

 

 

 

 

궁금한점 혹은 모르는점 어떤질문이든 댓글은 언제나 환영입니다.

 

 

'LeetCode' 카테고리의 다른 글

[LeetCode] 135. Candy  (0) 2023.08.26
[LeetCode] 115. Distinct Subsequences  (0) 2023.08.26
LeetCode  (0) 2022.11.11
LeetCode 74 Search a 2D Matrix  (0) 2021.11.17
LeetCode 112 Path Sum  (0) 2021.11.14