-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.php
35 lines (30 loc) · 977 Bytes
/
Solution.php
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
class Solution {
/**
* @param string[][] $board
* @return boolean
*/
function isValidSudoku($board) {
$rows = [];
$columns = [];
$boxes = [];
for ($i = 0; $i < 9; $i++) {
$rows[$i] = [];
$columns[$i] = [];
$boxes[$i] = [];
}
for ($row = 0; $row < 9; $row++) {
for ($column = 0; $column < 9; $column++) {
$cell = $board[$row][$column];
if ($cell != '.') {
if (in_array($cell, $rows[$row]) || in_array($cell, $columns[$column]) || in_array($cell, $boxes[floor($row / 3) * 3 + floor($column / 3)])) {
return false;
}
$rows[$row][] = $cell;
$columns[$column][] = $cell;
$boxes[floor($row / 3) * 3 + floor($column / 3)][] = $cell;
}
}
}
return true;
}
}