-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path036-valid-sudoku.cpp
More file actions
73 lines (60 loc) · 2.42 KB
/
Copy path036-valid-sudoku.cpp
File metadata and controls
73 lines (60 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// 36. 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.
//
// Tags: Hash Table
//
// https://leetcode.com/problems/valid-sudoku/
#include <iostream>
#include <gtest/gtest.h>
#include <string>
#include <vector>
using namespace std;
class Solution {
public:
bool isValidSudoku(vector<string>& board) {
int row[9][10] = {0};
int col[9][10] = {0};
int block[3][3][10] = {0};
for(int i = 0; i < 9; i++){
for(int j = 0; j < 9; j++){
if(board[i][j] == '.') continue;
int n = board[i][j] - '0';
if(row[i][n] || col[j][n] || block[i/3][j/3][n]){
return false;
}else{
row[i][n] ++;
col[j][n] ++;
block[i/3][j/3][n] ++;
}
}
}
return true;
}
};
TEST(leetcode_036_valid_sudoku, Basic)
{
Solution *solution = new Solution();
vector<string> board = {".87654321","2........","3........","4........","5........","6........","7........","8........","9........"};
EXPECT_TRUE(solution->isValidSudoku(board));
board = {"53..7....","6..195...",".98....6.","8...6...3","4..8.3..1","7...2...6",".6....28.","...419..5","....8..79"};
EXPECT_TRUE(solution->isValidSudoku(board));
board = {".........",".........",".........",".........",".........",".........",".........",".........","........."};
EXPECT_TRUE(solution->isValidSudoku(board));
board = {".1.......",".........",".........",".........",".........",".........",".........",".........",".1......."};
EXPECT_FALSE(solution->isValidSudoku(board));
board = {"1........",".........",".1.......",".........",".........",".........",".........",".........","........."};
EXPECT_FALSE(solution->isValidSudoku(board));
}
int main(int argc, char *argv[]) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}