Skip to content

Commit 84afb8f

Browse files
committed
day 20
1 parent e2f489c commit 84afb8f

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
Valid Sudoku
3+
============
4+
5+
Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:
6+
7+
Each row must contain the digits 1-9 without repetition.
8+
Each column must contain the digits 1-9 without repetition.
9+
Each of the nine 3 x 3 sub-boxes of the grid must contain the digits 1-9 without repetition.
10+
Note:
11+
12+
A Sudoku board (partially filled) could be valid but is not necessarily solvable.
13+
Only the filled cells need to be validated according to the mentioned rules.
14+
15+
Example 1:
16+
Input: board =
17+
[["5","3",".",".","7",".",".",".","."]
18+
,["6",".",".","1","9","5",".",".","."]
19+
,[".","9","8",".",".",".",".","6","."]
20+
,["8",".",".",".","6",".",".",".","3"]
21+
,["4",".",".","8",".","3",".",".","1"]
22+
,["7",".",".",".","2",".",".",".","6"]
23+
,[".","6",".",".",".",".","2","8","."]
24+
,[".",".",".","4","1","9",".",".","5"]
25+
,[".",".",".",".","8",".",".","7","9"]]
26+
Output: true
27+
28+
Example 2:
29+
Input: board =
30+
[["8","3",".",".","7",".",".",".","."]
31+
,["6",".",".","1","9","5",".",".","."]
32+
,[".","9","8",".",".",".",".","6","."]
33+
,["8",".",".",".","6",".",".",".","3"]
34+
,["4",".",".","8",".","3",".",".","1"]
35+
,["7",".",".",".","2",".",".",".","6"]
36+
,[".","6",".",".",".",".","2","8","."]
37+
,[".",".",".","4","1","9",".",".","5"]
38+
,[".",".",".",".","8",".",".","7","9"]]
39+
Output: false
40+
Explanation: Same as Example 1, except with the 5 in the top left corner being modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid.
41+
42+
Constraints:
43+
board.length == 9
44+
board[i].length == 9
45+
board[i][j] is a digit or '.'.
46+
*/
47+
48+
class Solution
49+
{
50+
public:
51+
bool valid(vector<vector<char>> &board, int r, int c, char val)
52+
{
53+
for (int i = 0; i < 9; ++i)
54+
if (board[i][c] == val || board[r][i] == val)
55+
{
56+
return false;
57+
}
58+
59+
for (int i = 0; i < 3; ++i)
60+
{
61+
for (int j = 0; j < 3; ++j)
62+
{
63+
if (board[(r / 3) * 3 + i][(c / 3) * 3 + j] == val)
64+
{
65+
return false;
66+
}
67+
}
68+
}
69+
70+
return true;
71+
}
72+
73+
bool isValidSudoku(vector<vector<char>> board)
74+
{
75+
for (int i = 0; i < board.size(); ++i)
76+
{
77+
for (int j = 0; j < board[0].size(); ++j)
78+
{
79+
80+
if (board[i][j] != '.')
81+
{
82+
char ch = board[i][j];
83+
board[i][j] = '!';
84+
85+
if (!valid(board, i, j, ch))
86+
{
87+
return false;
88+
}
89+
90+
board[i][j] = ch;
91+
}
92+
}
93+
}
94+
95+
return true;
96+
}
97+
};

Leetcode Daily Challenge/August-2021/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@
2121
| 17. | [Count Good Nodes in Binary Tree](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/615/week-3-august-15th-august-21st/3899/) | [cpp](./17.%20Count%20Good%20Nodes%20in%20Binary%20Tree.cpp) |
2222
| 18. | [Decode Ways](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/615/week-3-august-15th-august-21st/3902/) | [cpp](./18.%20Decode%20Ways.cpp) |
2323
| 19. | [Maximum Product of Splitted Binary Tree](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/615/week-3-august-15th-august-21st/3903/) | [cpp](./19.%20Maximum%20Product%20of%20Splitted%20Binary%20Tree.cpp) |
24+
| 20. | [Valid Sudoku](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/615/week-3-august-15th-august-21st/3904/) | [cpp](./20.%20Valid%20Sudoku.cpp) |

0 commit comments

Comments
 (0)