LEETCODE:有趣的数独

地址 https://leetcode-cn.com/problems/valid-sudoku/

对每块区域标0-8
为什么用(i/3)3+j/3
因为跨行的时候要多一倍 (i/3)
3-((i-1)/3)*3 i在边界时 会少3.

code:

class Solution {
public:
    bool isValidSudoku(vector<vector<char>>& board) {
        int row[9][10] = {0};
        int col[9][10] = {0};
        int box[9][10] = {0};
        for(int i=0; i<9; i++){
            for(int j = 0; j<9; j++)
            {
                if(board[i][j] == '.') continue;
                int curNumber = board[i][j]-'0';
                if(row[i][curNumber]) return false; 
                if(col[j][curNumber]) return false;
                if(box[j/3 + (i/3)*3][curNumber]) return false;
                row[i][curNumber] = 1;
                col[j][curNumber] = 1;
                box[j/3 + (i/3)*3][curNumber] = 1;
            }
        }
        return true;
    }
};

猜你喜欢

转载自www.cnblogs.com/Hunter01/p/12527655.html