https://oj.leetcode.com/problems/valid-sudoku/
给出数独的一部分来,验证给出的这一部分是否为 valid
行上无重复的,且是从 ‘1’ 到‘9’
列上无重复的,且是从 ‘1’ 到‘9’
小的3*3 无重复的,且是从 ‘1’ 到‘9’
class Solution { public: bool isValidSudoku(vector<vector<char> > &board) { if(board.empty() || board.size() == 0) return false; // store the filled to map for(int i = 0; i < 9; i++) { for(int j = 0; j < 9; j++) { int times = 0; // record the same // is num if(board[i][j] != '.') { // from 1 to 9 if(!(board[i][j] >= '1' && board[i][j] <= '9')) return false; // small cube int ith = i / 3; int jth = j / 3; for(int p = 0; p < 3; p++) { for(int q = 0; q < 3; q++) { if(board[ith*3 + p][jth*3 + q] == board[i][j]) times++; } } for(int p = 0; p < 9; p++) { if(board[i][j] == board[i][p]) times++; if(board[i][j] == board[p][j]) times++; } } if(times > 3) return false; } } return true; } };