[LeetCode] Valid Sudoku

This problem is relatively easy. The challenge is how to get a concise code. This post shares a very elegant one which traverses each cell of board only once. The code is rewritten below.

 1 class Solution {
 2 public:
 3     bool isValidSudoku(vector<vector<char>>& board) {
 4         bool row[9][9] = {false}, col[9][9] = {false}, box[9][9] = {false};
 5         for (int i = 0; i < 9; i++) {
 6             for (int j = 0; j < 9; j++) {
 7                 if (board[i][j] != '.') {
 8                     int num = board[i][j] - '0' - 1, k = i / 3 * 3 + j / 3;
 9                     if (row[i][num] || col[j][num] || box[k][num]) return false;
10                     row[i][num] = col[j][num] = box[k][num] = true;
11                 }
12             }
13         }
14         return true;
15     }
16 };
原文地址:https://www.cnblogs.com/jcliBlogger/p/4792475.html