Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3337bf3

Browse files
feat: add php solution to lc problems: No.0036-0040 (doocs#2539)
1 parent ce9f63c commit 3337bf3

File tree

15 files changed

+689
-0
lines changed

15 files changed

+689
-0
lines changed

‎solution/0000-0099/0036.Valid Sudoku/README.md‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,48 @@ var isValidSudoku = function (board) {
240240
};
241241
```
242242

243+
```php
244+
class Solution {
245+
/**
246+
* @param string[][] $board
247+
* @return boolean
248+
*/
249+
250+
function isValidSudoku($board) {
251+
$rows = [];
252+
$columns = [];
253+
$boxes = [];
254+
255+
for ($i = 0; $i < 9; $i++) {
256+
$rows[$i] = [];
257+
$columns[$i] = [];
258+
$boxes[$i] = [];
259+
}
260+
261+
for ($row = 0; $row < 9; $row++) {
262+
for ($column = 0; $column < 9; $column++) {
263+
$cell = $board[$row][$column];
264+
265+
if ($cell != '.') {
266+
if (
267+
in_array($cell, $rows[$row]) ||
268+
in_array($cell, $columns[$column]) ||
269+
in_array($cell, $boxes[floor($row / 3) * 3 + floor($column / 3)])
270+
) {
271+
return false;
272+
}
273+
274+
$rows[$row][] = $cell;
275+
$columns[$column][] = $cell;
276+
$boxes[floor($row / 3) * 3 + floor($column / 3)][] = $cell;
277+
}
278+
}
279+
}
280+
return true;
281+
}
282+
}
283+
```
284+
243285
<!-- tabs:end -->
244286

245287
<!-- end -->

‎solution/0000-0099/0036.Valid Sudoku/README_EN.md‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,48 @@ var isValidSudoku = function (board) {
234234
};
235235
```
236236

237+
```php
238+
class Solution {
239+
/**
240+
* @param string[][] $board
241+
* @return boolean
242+
*/
243+
244+
function isValidSudoku($board) {
245+
$rows = [];
246+
$columns = [];
247+
$boxes = [];
248+
249+
for ($i = 0; $i < 9; $i++) {
250+
$rows[$i] = [];
251+
$columns[$i] = [];
252+
$boxes[$i] = [];
253+
}
254+
255+
for ($row = 0; $row < 9; $row++) {
256+
for ($column = 0; $column < 9; $column++) {
257+
$cell = $board[$row][$column];
258+
259+
if ($cell != '.') {
260+
if (
261+
in_array($cell, $rows[$row]) ||
262+
in_array($cell, $columns[$column]) ||
263+
in_array($cell, $boxes[floor($row / 3) * 3 + floor($column / 3)])
264+
) {
265+
return false;
266+
}
267+
268+
$rows[$row][] = $cell;
269+
$columns[$column][] = $cell;
270+
$boxes[floor($row / 3) * 3 + floor($column / 3)][] = $cell;
271+
}
272+
}
273+
}
274+
return true;
275+
}
276+
}
277+
```
278+
237279
<!-- tabs:end -->
238280

239281
<!-- end -->
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
/**
3+
* @param string[][] $board
4+
* @return boolean
5+
*/
6+
7+
function isValidSudoku($board) {
8+
$rows = [];
9+
$columns = [];
10+
$boxes = [];
11+
12+
for ($i = 0; $i < 9; $i++) {
13+
$rows[$i] = [];
14+
$columns[$i] = [];
15+
$boxes[$i] = [];
16+
}
17+
18+
for ($row = 0; $row < 9; $row++) {
19+
for ($column = 0; $column < 9; $column++) {
20+
$cell = $board[$row][$column];
21+
22+
if ($cell != '.') {
23+
if (in_array($cell, $rows[$row]) || in_array($cell, $columns[$column]) || in_array($cell, $boxes[floor($row / 3) * 3 + floor($column / 3)])) {
24+
return false;
25+
}
26+
27+
$rows[$row][] = $cell;
28+
$columns[$column][] = $cell;
29+
$boxes[floor($row / 3) * 3 + floor($column / 3)][] = $cell;
30+
}
31+
}
32+
}
33+
return true;
34+
}
35+
}

‎solution/0000-0099/0037.Sudoku Solver/README.md‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,84 @@ public class Solution {
356356
}
357357
```
358358

359+
```php
360+
class Solution {
361+
/**
362+
* @param string[][] $board
363+
* @return bool
364+
*/
365+
366+
public function solveSudoku(&$board) {
367+
if (isSolved($board)) {
368+
return true;
369+
}
370+
371+
$emptyCell = findEmptyCell($board);
372+
$row = $emptyCell[0];
373+
$col = $emptyCell[1];
374+
375+
for ($num = 1; $num <= 9; $num++) {
376+
if (isValid($board, $row, $col, $num)) {
377+
$board[$row][$col] = (string) $num;
378+
if ($this->solveSudoku($board)) {
379+
return true;
380+
}
381+
$board[$row][$col] = '.';
382+
}
383+
}
384+
return false;
385+
}
386+
}
387+
388+
function isSolved($board) {
389+
foreach ($board as $row) {
390+
if (in_array('.', $row)) {
391+
return false;
392+
}
393+
}
394+
return true;
395+
}
396+
397+
function findEmptyCell($board) {
398+
for ($row = 0; $row < 9; $row++) {
399+
for ($col = 0; $col < 9; $col++) {
400+
if ($board[$row][$col] === '.') {
401+
return [$row, $col];
402+
}
403+
}
404+
}
405+
406+
return null;
407+
}
408+
409+
function isValid($board, $row, $col, $num) {
410+
for ($i = 0; $i < 9; $i++) {
411+
if ($board[$row][$i] == $num) {
412+
return false;
413+
}
414+
}
415+
416+
for ($i = 0; $i < 9; $i++) {
417+
if ($board[$i][$col] == $num) {
418+
return false;
419+
}
420+
}
421+
422+
$startRow = floor($row / 3) * 3;
423+
$endCol = floor($col / 3) * 3;
424+
425+
for ($i = 0; $i < 3; $i++) {
426+
for ($j = 0; $j < 3; $j++) {
427+
if ($board[$startRow + $i][$endCol + $j] == $num) {
428+
return false;
429+
}
430+
}
431+
}
432+
433+
return true;
434+
}
435+
```
436+
359437
<!-- tabs:end -->
360438

361439
<!-- end -->

‎solution/0000-0099/0037.Sudoku Solver/README_EN.md‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,84 @@ public class Solution {
346346
}
347347
```
348348

349+
```php
350+
class Solution {
351+
/**
352+
* @param string[][] $board
353+
* @return bool
354+
*/
355+
356+
public function solveSudoku(&$board) {
357+
if (isSolved($board)) {
358+
return true;
359+
}
360+
361+
$emptyCell = findEmptyCell($board);
362+
$row = $emptyCell[0];
363+
$col = $emptyCell[1];
364+
365+
for ($num = 1; $num <= 9; $num++) {
366+
if (isValid($board, $row, $col, $num)) {
367+
$board[$row][$col] = (string) $num;
368+
if ($this->solveSudoku($board)) {
369+
return true;
370+
}
371+
$board[$row][$col] = '.';
372+
}
373+
}
374+
return false;
375+
}
376+
}
377+
378+
function isSolved($board) {
379+
foreach ($board as $row) {
380+
if (in_array('.', $row)) {
381+
return false;
382+
}
383+
}
384+
return true;
385+
}
386+
387+
function findEmptyCell($board) {
388+
for ($row = 0; $row < 9; $row++) {
389+
for ($col = 0; $col < 9; $col++) {
390+
if ($board[$row][$col] === '.') {
391+
return [$row, $col];
392+
}
393+
}
394+
}
395+
396+
return null;
397+
}
398+
399+
function isValid($board, $row, $col, $num) {
400+
for ($i = 0; $i < 9; $i++) {
401+
if ($board[$row][$i] == $num) {
402+
return false;
403+
}
404+
}
405+
406+
for ($i = 0; $i < 9; $i++) {
407+
if ($board[$i][$col] == $num) {
408+
return false;
409+
}
410+
}
411+
412+
$startRow = floor($row / 3) * 3;
413+
$endCol = floor($col / 3) * 3;
414+
415+
for ($i = 0; $i < 3; $i++) {
416+
for ($j = 0; $j < 3; $j++) {
417+
if ($board[$startRow + $i][$endCol + $j] == $num) {
418+
return false;
419+
}
420+
}
421+
}
422+
423+
return true;
424+
}
425+
```
426+
349427
<!-- tabs:end -->
350428

351429
<!-- end -->
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
class Solution {
2+
/**
3+
* @param string[][] $board
4+
* @return bool
5+
*/
6+
7+
public function solveSudoku(&$board) {
8+
if (isSolved($board)) {
9+
return true;
10+
}
11+
12+
$emptyCell = findEmptyCell($board);
13+
$row = $emptyCell[0];
14+
$col = $emptyCell[1];
15+
16+
for ($num = 1; $num <= 9; $num++) {
17+
if (isValid($board, $row, $col, $num)) {
18+
$board[$row][$col] = (string) $num;
19+
if ($this->solveSudoku($board)) {
20+
return true;
21+
}
22+
$board[$row][$col] = '.';
23+
}
24+
}
25+
return false;
26+
}
27+
}
28+
29+
function isSolved($board) {
30+
foreach ($board as $row) {
31+
if (in_array('.', $row)) {
32+
return false;
33+
}
34+
}
35+
return true;
36+
}
37+
38+
function findEmptyCell($board) {
39+
for ($row = 0; $row < 9; $row++) {
40+
for ($col = 0; $col < 9; $col++) {
41+
if ($board[$row][$col] === '.') {
42+
return [$row, $col];
43+
}
44+
}
45+
}
46+
47+
return null;
48+
}
49+
50+
function isValid($board, $row, $col, $num) {
51+
for ($i = 0; $i < 9; $i++) {
52+
if ($board[$row][$i] == $num) {
53+
return false;
54+
}
55+
}
56+
57+
for ($i = 0; $i < 9; $i++) {
58+
if ($board[$i][$col] == $num) {
59+
return false;
60+
}
61+
}
62+
63+
$startRow = floor($row / 3) * 3;
64+
$endCol = floor($col / 3) * 3;
65+
66+
for ($i = 0; $i < 3; $i++) {
67+
for ($j = 0; $j < 3; $j++) {
68+
if ($board[$startRow + $i][$endCol + $j] == $num) {
69+
return false;
70+
}
71+
}
72+
}
73+
74+
return true;
75+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /