Leetcode 36. Valid Sudoku
阿新 • • 發佈:2018-12-17
文章作者:Tyan 部落格:noahsnail.com | CSDN | 簡書
1. Description
2. Solution
class Solution { public: bool isValidSudoku(vector<vector<char>>& board) { return validate(board); } private: bool validate(vector<vector<char>>& board) { int n = board.size(); vector<vector<unordered_map<char, char>>> subboxes(3, vector<unordered_map<char, char>>(3)); for(int i = 0; i < n; i++) { unordered_map<char, char> row; unordered_map<char, char> column; for(int j = 0; j < n; j++) { if(board[i][j] != '.') { if(row.find(board[i][j]) != row.end()) { return false; } else { row[board[i][j]] = board[i][j]; } if(subboxes[i / 3][j / 3].find(board[i][j]) != row.end()) { return false; } else { subboxes[i / 3][j / 3][board[i][j]] = board[i][j]; } } if(board[j][i] != '.') { if(column.find(board[j][i]) != column.end()) { return false; } else { column[board[j][i]] = board[j][i]; } } } } return true; } };