|
37 | 37 | <p><strong class="example">Example 1:</strong></p>
|
38 | 38 | <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" />
|
39 | 39 | <pre>
|
40 |
| -<strong>Input:</strong> board = |
| 40 | +<strong>Input:</strong> board = |
41 | 41 | [["5","3",".",".","7",".",".",".","."]
|
42 | 42 | ,["6",".",".","1","9","5",".",".","."]
|
43 | 43 | ,[".","9","8",".",".",".",".","6","."]
|
|
53 | 53 | <p><strong class="example">Example 2:</strong></p>
|
54 | 54 |
|
55 | 55 | <pre>
|
56 |
| -<strong>Input:</strong> board = |
| 56 | +<strong>Input:</strong> board = |
57 | 57 | [["8","3",".",".","7",".",".",".","."]
|
58 | 58 | ,["6",".",".","1","9","5",".",".","."]
|
59 | 59 | ,[".","9","8",".",".",".",".","6","."]
|
@@ -232,6 +232,36 @@ function isValidSudoku(board: string[][]): boolean {
|
232 | 232 | }
|
233 | 233 | ```
|
234 | 234 |
|
| 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 | + |
235 | 265 | #### JavaScript
|
236 | 266 |
|
237 | 267 | ```js
|
@@ -262,43 +292,63 @@ var isValidSudoku = function (board) {
|
262 | 292 | };
|
263 | 293 | ```
|
264 | 294 |
|
| 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 | + |
265 | 325 | #### PHP
|
266 | 326 |
|
267 | 327 | ```php
|
268 | 328 | class Solution {
|
269 | 329 | /**
|
270 |
| - * @param string[][] $board |
271 |
| - * @return boolean |
| 330 | + * @param String[][] $board |
| 331 | + * @return Boolean |
272 | 332 | */
|
273 |
| - |
274 | 333 | 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)); |
278 | 337 |
|
279 | 338 | 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; |
301 | 348 | }
|
| 349 | + $row[$i][$num] = true; |
| 350 | + $col[$j][$num] = true; |
| 351 | + $sub[$k][$num] = true; |
302 | 352 | }
|
303 | 353 | }
|
304 | 354 | return true;
|
|
0 commit comments