Skip to content

Commit 0651e16

Browse files
authored
feat: add solutions to lc problem: No.0036 (#4676)
No.0036.Valid Sudoku
1 parent 7c89a76 commit 0651e16

File tree

5 files changed

+225
-81
lines changed

5 files changed

+225
-81
lines changed

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

Lines changed: 79 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ tags:
4141
<p><strong>示例 1:</strong></p>
4242
<img src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0036.Valid%20Sudoku/images/250px-sudoku-by-l2g-20050714svg.png" style="height:250px; width:250px" />
4343
<pre>
44-
<strong>输入:</strong>board =
44+
<strong>输入:</strong>board =
4545
[["5","3",".",".","7",".",".",".","."]
4646
,["6",".",".","1","9","5",".",".","."]
4747
,[".","9","8",".",".",".",".","6","."]
@@ -57,7 +57,7 @@ tags:
5757
<p><strong>示例 2:</strong></p>
5858

5959
<pre>
60-
<strong>输入:</strong>board =
60+
<strong>输入:</strong>board =
6161
[["8","3",".",".","7",".",".",".","."]
6262
,["6",".",".","1","9","5",".",".","."]
6363
,[".","9","8",".",".",".",".","6","."]
@@ -236,6 +236,36 @@ function isValidSudoku(board: string[][]): boolean {
236236
}
237237
```
238238

239+
#### Rust
240+
241+
```rust
242+
impl Solution {
243+
pub fn is_valid_sudoku(board: Vec<Vec<char>>) -> bool {
244+
let mut row = vec![vec![false; 9]; 9];
245+
let mut col = vec![vec![false; 9]; 9];
246+
let mut sub = vec![vec![false; 9]; 9];
247+
248+
for i in 0..9 {
249+
for j in 0..9 {
250+
let c = board[i][j];
251+
if c == '.' {
252+
continue;
253+
}
254+
let num = (c as u8 - b'0' - 1) as usize;
255+
let k = i / 3 * 3 + j / 3;
256+
if row[i][num] || col[j][num] || sub[k][num] {
257+
return false;
258+
}
259+
row[i][num] = true;
260+
col[j][num] = true;
261+
sub[k][num] = true;
262+
}
263+
}
264+
true
265+
}
266+
}
267+
```
268+
239269
#### JavaScript
240270

241271
```js
@@ -266,43 +296,63 @@ var isValidSudoku = function (board) {
266296
};
267297
```
268298

299+
#### C#
300+
301+
```cs
302+
public class Solution {
303+
public bool IsValidSudoku(char[][] board) {
304+
bool[,] row = new bool[9, 9];
305+
bool[,] col = new bool[9, 9];
306+
bool[,] sub = new bool[9, 9];
307+
308+
for (int i = 0; i < 9; i++) {
309+
for (int j = 0; j < 9; j++) {
310+
char c = board[i][j];
311+
if (c == '.') {
312+
continue;
313+
}
314+
int num = c - '0' - 1;
315+
int k = (i / 3) * 3 + (j / 3);
316+
if (row[i, num] || col[j, num] || sub[k, num]) {
317+
return false;
318+
}
319+
row[i, num] = true;
320+
col[j, num] = true;
321+
sub[k, num] = true;
322+
}
323+
}
324+
return true;
325+
}
326+
}
327+
```
328+
269329
#### PHP
270330

271331
```php
272332
class Solution {
273333
/**
274-
* @param string[][] $board
275-
* @return boolean
334+
* @param String[][] $board
335+
* @return Boolean
276336
*/
277-
278337
function isValidSudoku($board) {
279-
$rows = [];
280-
$columns = [];
281-
$boxes = [];
338+
$row = array_fill(0, 9, array_fill(0, 9, false));
339+
$col = array_fill(0, 9, array_fill(0, 9, false));
340+
$sub = array_fill(0, 9, array_fill(0, 9, false));
282341

283342
for ($i = 0; $i < 9; $i++) {
284-
$rows[$i] = [];
285-
$columns[$i] = [];
286-
$boxes[$i] = [];
287-
}
288-
289-
for ($row = 0; $row < 9; $row++) {
290-
for ($column = 0; $column < 9; $column++) {
291-
$cell = $board[$row][$column];
292-
293-
if ($cell != '.') {
294-
if (
295-
in_array($cell, $rows[$row]) ||
296-
in_array($cell, $columns[$column]) ||
297-
in_array($cell, $boxes[floor($row / 3) * 3 + floor($column / 3)])
298-
) {
299-
return false;
300-
}
301-
302-
$rows[$row][] = $cell;
303-
$columns[$column][] = $cell;
304-
$boxes[floor($row / 3) * 3 + floor($column / 3)][] = $cell;
343+
for ($j = 0; $j < 9; $j++) {
344+
$c = $board[$i][$j];
345+
if ($c === '.') {
346+
continue;
347+
}
348+
$num = intval($c) - 1;
349+
$k = intdiv($i, 3) * 3 + intdiv($j, 3);
350+
if ($row[$i][$num] || $col[$j][$num] || $sub[$k][$num]) {
351+
return false;
305352
}
353+
$row[$i][$num] = true;
354+
$col[$j][$num] = true;
355+
$sub[$k][$num] = true;
306356
}
307357
}
308358
return true;

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

Lines changed: 79 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ tags:
3737
<p><strong class="example">Example 1:</strong></p>
3838
<img src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0036.Valid%20Sudoku/images/250px-Sudoku-by-L2G-20050714.svg.png" style="height:250px; width:250px" />
3939
<pre>
40-
<strong>Input:</strong> board =
40+
<strong>Input:</strong> board =
4141
[[&quot;5&quot;,&quot;3&quot;,&quot;.&quot;,&quot;.&quot;,&quot;7&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;]
4242
,[&quot;6&quot;,&quot;.&quot;,&quot;.&quot;,&quot;1&quot;,&quot;9&quot;,&quot;5&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;]
4343
,[&quot;.&quot;,&quot;9&quot;,&quot;8&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;6&quot;,&quot;.&quot;]
@@ -53,7 +53,7 @@ tags:
5353
<p><strong class="example">Example 2:</strong></p>
5454

5555
<pre>
56-
<strong>Input:</strong> board =
56+
<strong>Input:</strong> board =
5757
[[&quot;8&quot;,&quot;3&quot;,&quot;.&quot;,&quot;.&quot;,&quot;7&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;]
5858
,[&quot;6&quot;,&quot;.&quot;,&quot;.&quot;,&quot;1&quot;,&quot;9&quot;,&quot;5&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;]
5959
,[&quot;.&quot;,&quot;9&quot;,&quot;8&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;6&quot;,&quot;.&quot;]
@@ -232,6 +232,36 @@ function isValidSudoku(board: string[][]): boolean {
232232
}
233233
```
234234

235+
#### Rust
236+
237+
```rust
238+
impl Solution {
239+
pub fn is_valid_sudoku(board: Vec<Vec<char>>) -> bool {
240+
let mut row = vec![vec![false; 9]; 9];
241+
let mut col = vec![vec![false; 9]; 9];
242+
let mut sub = vec![vec![false; 9]; 9];
243+
244+
for i in 0..9 {
245+
for j in 0..9 {
246+
let c = board[i][j];
247+
if c == '.' {
248+
continue;
249+
}
250+
let num = (c as u8 - b'0' - 1) as usize;
251+
let k = i / 3 * 3 + j / 3;
252+
if row[i][num] || col[j][num] || sub[k][num] {
253+
return false;
254+
}
255+
row[i][num] = true;
256+
col[j][num] = true;
257+
sub[k][num] = true;
258+
}
259+
}
260+
true
261+
}
262+
}
263+
```
264+
235265
#### JavaScript
236266

237267
```js
@@ -262,43 +292,63 @@ var isValidSudoku = function (board) {
262292
};
263293
```
264294

295+
#### C#
296+
297+
```cs
298+
public class Solution {
299+
public bool IsValidSudoku(char[][] board) {
300+
bool[,] row = new bool[9, 9];
301+
bool[,] col = new bool[9, 9];
302+
bool[,] sub = new bool[9, 9];
303+
304+
for (int i = 0; i < 9; i++) {
305+
for (int j = 0; j < 9; j++) {
306+
char c = board[i][j];
307+
if (c == '.') {
308+
continue;
309+
}
310+
int num = c - '0' - 1;
311+
int k = (i / 3) * 3 + (j / 3);
312+
if (row[i, num] || col[j, num] || sub[k, num]) {
313+
return false;
314+
}
315+
row[i, num] = true;
316+
col[j, num] = true;
317+
sub[k, num] = true;
318+
}
319+
}
320+
return true;
321+
}
322+
}
323+
```
324+
265325
#### PHP
266326

267327
```php
268328
class Solution {
269329
/**
270-
* @param string[][] $board
271-
* @return boolean
330+
* @param String[][] $board
331+
* @return Boolean
272332
*/
273-
274333
function isValidSudoku($board) {
275-
$rows = [];
276-
$columns = [];
277-
$boxes = [];
334+
$row = array_fill(0, 9, array_fill(0, 9, false));
335+
$col = array_fill(0, 9, array_fill(0, 9, false));
336+
$sub = array_fill(0, 9, array_fill(0, 9, false));
278337

279338
for ($i = 0; $i < 9; $i++) {
280-
$rows[$i] = [];
281-
$columns[$i] = [];
282-
$boxes[$i] = [];
283-
}
284-
285-
for ($row = 0; $row < 9; $row++) {
286-
for ($column = 0; $column < 9; $column++) {
287-
$cell = $board[$row][$column];
288-
289-
if ($cell != '.') {
290-
if (
291-
in_array($cell, $rows[$row]) ||
292-
in_array($cell, $columns[$column]) ||
293-
in_array($cell, $boxes[floor($row / 3) * 3 + floor($column / 3)])
294-
) {
295-
return false;
296-
}
297-
298-
$rows[$row][] = $cell;
299-
$columns[$column][] = $cell;
300-
$boxes[floor($row / 3) * 3 + floor($column / 3)][] = $cell;
339+
for ($j = 0; $j < 9; $j++) {
340+
$c = $board[$i][$j];
341+
if ($c === '.') {
342+
continue;
343+
}
344+
$num = intval($c) - 1;
345+
$k = intdiv($i, 3) * 3 + intdiv($j, 3);
346+
if ($row[$i][$num] || $col[$j][$num] || $sub[$k][$num]) {
347+
return false;
301348
}
349+
$row[$i][$num] = true;
350+
$col[$j][$num] = true;
351+
$sub[$k][$num] = true;
302352
}
303353
}
304354
return true;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
public class Solution {
2+
public bool IsValidSudoku(char[][] board) {
3+
bool[,] row = new bool[9, 9];
4+
bool[,] col = new bool[9, 9];
5+
bool[,] sub = new bool[9, 9];
6+
7+
for (int i = 0; i < 9; i++) {
8+
for (int j = 0; j < 9; j++) {
9+
char c = board[i][j];
10+
if (c == '.') {
11+
continue;
12+
}
13+
int num = c - '0' - 1;
14+
int k = (i / 3) * 3 + (j / 3);
15+
if (row[i, num] || col[j, num] || sub[k, num]) {
16+
return false;
17+
}
18+
row[i, num] = true;
19+
col[j, num] = true;
20+
sub[k, num] = true;
21+
}
22+
}
23+
return true;
24+
}
25+
}

solution/0000-0099/0036.Valid Sudoku/Solution.php

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,27 @@
11
class Solution {
22
/**
3-
* @param string[][] $board
4-
* @return boolean
3+
* @param String[][] $board
4+
* @return Boolean
55
*/
6-
76
function isValidSudoku($board) {
8-
$rows = [];
9-
$columns = [];
10-
$boxes = [];
7+
$row = array_fill(0, 9, array_fill(0, 9, false));
8+
$col = array_fill(0, 9, array_fill(0, 9, false));
9+
$sub = array_fill(0, 9, array_fill(0, 9, false));
1110

1211
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;
12+
for ($j = 0; $j < 9; $j++) {
13+
$c = $board[$i][$j];
14+
if ($c === '.') {
15+
continue;
16+
}
17+
$num = intval($c) - 1;
18+
$k = intdiv($i, 3) * 3 + intdiv($j, 3);
19+
if ($row[$i][$num] || $col[$j][$num] || $sub[$k][$num]) {
20+
return false;
3021
}
22+
$row[$i][$num] = true;
23+
$col[$j][$num] = true;
24+
$sub[$k][$num] = true;
3125
}
3226
}
3327
return true;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
impl Solution {
2+
pub fn is_valid_sudoku(board: Vec<Vec<char>>) -> bool {
3+
let mut row = vec![vec![false; 9]; 9];
4+
let mut col = vec![vec![false; 9]; 9];
5+
let mut sub = vec![vec![false; 9]; 9];
6+
7+
for i in 0..9 {
8+
for j in 0..9 {
9+
let c = board[i][j];
10+
if c == '.' {
11+
continue;
12+
}
13+
let num = (c as u8 - b'0' - 1) as usize;
14+
let k = i / 3 * 3 + j / 3;
15+
if row[i][num] || col[j][num] || sub[k][num] {
16+
return false;
17+
}
18+
row[i][num] = true;
19+
col[j][num] = true;
20+
sub[k][num] = true;
21+
}
22+
}
23+
true
24+
}
25+
}

0 commit comments

Comments
 (0)