[email protected] [36] Valid Sudoku

https://leetcode.com/problems/valid-sudoku/

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.

The Sudoku board could be partially filled, where empty cells are filled with the character ‘.‘.

A partially filled sudoku which is valid.

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

class Solution {
public:
    vector<int> getIdx(int i, int j) {
        vector<int> idx(4);
        int row, col;
        if(i>=0 && i<=2) {
            idx[0] = 0; idx[1] = 2;
        }
        else if(i>=3 && i<=5) {
            idx[0] = 3; idx[1] = 5;
        }
        else if(i>=6 && i<=8) {
            idx[0] = 6; idx[1] = 8;
        }

        if(j>=0 && j<=2) {
            idx[2] = 0; idx[3] = 2;
        }
        else if(j>=3 && j<=5) {
            idx[2] = 3; idx[3] = 5;
        }
        else if(j>=6 && j<=8) {
            idx[2] = 6; idx[3] = 8;
        }

        return idx;
    }
    bool checkRowAndColumn(vector<vector<char>>& board, int i, int j) {
        if(i<0 || i>=board.size() || j<0 || j>=board[0].size()) return false;

        for(int ni=0;ni<board.size();++ni) {
            if(ni == i || board[ni][j] == ‘.‘) continue;
            if(board[ni][j] == board[i][j]) return false;
        }

        for(int nj=0;nj<board[0].size();++nj) {
            if(nj == j || board[i][nj] == ‘.‘) continue;
            if(board[i][nj] == board[i][j]) return false;
        }

        return true;
    }

    bool checkLocal(vector<vector<char>>& board, int i, int j) {
        if(i<0 || i>=board.size() || j<0 || j>=board[0].size()) return false;

        vector<int> idx = getIdx(i, j);
        int li = idx[0], ri = idx[1], lj = idx[2], rj = idx[3];

        for(int p=li;p<=ri;++p) {
            for(int q=lj;q<=rj;++q) {
                if((i==p && j==q) || board[p][q] == ‘.‘) continue;
                if(board[i][j] == board[p][q]) return false;
            }
        }

        return true;
    }
    bool isValidSudoku(vector<vector<char>>& board) {
        for(int i=0;i<board.size();++i) {
            for(int j=0;j<board[i].size();++j) {
                if(board[i][j] == ‘.‘) continue;
                if(!checkLocal(board, i, j) || !checkRowAndColumn(board, i, j)) return false;
            }
        }
        return true;
    }
};

时间: 2024-10-13 16:07:51

[email protected] [36] Valid Sudoku的相关文章

[Leetcode][Python]36: Valid Sudoku

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 36: Valid Sudokuhttps://oj.leetcode.com/problems/valid-sudoku/ Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty

leetCode 36. Valid Sudoku(数独) 哈希

36. Valid Sudoku(合法数独) Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. Note:A valid S

【LeetCode】36 - Valid Sudoku

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.(http://sudoku.com.au/TheRules.aspx) The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. N

leetCode 36.Valid Sudoku(有效的数独) 解题思路和方法

Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. Note: A valid Sudoku boa

LeetCode 36 Valid Sudoku (C,C++,Java,Python)

Problem: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. Note: A valid Sudoku board (

36. Valid Sudoku 有效的数独

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. Note: A valid Sudoku board (partially

[leedcode 36] Valid Sudoku

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. public class Solution { public boolea

LeetCode Medium: 36. Valid Sudoku

一.题目 Determine if a 9x9 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 t

36. Valid Sudoku/37. Sudoku Solver - 数独问题-- backtracking 经典

题意: 经典的递归题, 要求:除了要求 横竖都填满 1~9外, 每个3*3也都要求满足 1~9 36. 数组可以部分填充, 问是否一个有效的 sudoku. 写了个好烧脑的 四重循环来check 3*3 的部分.  重点在于 用数组作为hash . 然后对于 check 3*3 部分, 其实就是9个小方块,  9个小方块 定点坐标为 [0 0] [0 3] [06] [3 0] [3 3 ] [3 6] [6 0] [ 6 3] [6 6] ,每个顶点递增为横竖 递增都 3 , 因此写了个二重循