【Leetcode】【Easy】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.

解题:

将每一行、每一列、每一个九格中的数字分别输入一个大小为9的数组中,记录输入的数字是否重复

代码:

 1 class Solution {
 2 public:
 3     bool isValidSudoku(vector<vector<char> > &board) {
 4         return isValidRow(board) && isValidColumn(board) && isValidBox(board);
 5     }
 6     
 7     bool isValidRow(vector<vector<char> > board) {
 8         int count[9];
 9         for (int i = 0; i < 9; ++i) {
10             memset(count, 0, sizeof(int) * 9);
11             for (int j = 0; j < 9; ++j) {
12                 if (!add(count, board[i][j]))
13                     return false;
14             }
15         }
16         return true;
17     }
18     
19     bool isValidColumn(vector<vector<char> > board) {
20         int count[9];
21         for (int i = 0; i < 9; ++i) {
22             memset(count, 0, sizeof(int) * 9);
23             for (int j = 0; j < 9; ++j) {
24                 if (!add(count, board[j][i]))
25                     return false;
26             }
27         }
28         return true;
29     }
30     
31     bool isValidBox(vector<vector<char> > board) {
32         int count[9];
33         int point[9][2] = {
34             {0, 0}, {0, 3}, {0, 6}, {3, 0}, {3, 3}, {3, 6}, {6, 0}, {6, 3}, {6, 6}
35         };
36         for (int i = 0; i < 9; ++i) {
37             memset(count, 0, sizeof(int) * 9);
38             for (int x = 0; x < 3; ++x) {
39                 for (int y = 0; y < 3; ++y) {
40                     int abscissa = point[i][0] + x;
41                     int ordinate = point[i][1] + y;
42                     if (!add(count, board[abscissa][ordinate]))
43                         return false;
44                 }
45             }
46         }
47         return true;
48     }
49     
50     bool add(int count[], char dig) {
51         if (dig == '.') 
52             return true;
53         else
54             return (++count[dig - '1']) == 1;
55     }
56     
57 };

代码疑问:

1、为什么形参设置为board和&board都可以通过编译;

附录:

数独填充算法

原文地址:https://www.cnblogs.com/huxiao-tee/p/4227387.html