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 95c6187

Browse files
Merge branch 'youngyangyang04:master' into remote
2 parents e2807eb + 361e992 commit 95c6187

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

‎pics/.DS_Store‎

-6 KB
Binary file not shown.

‎problems/0037.解数独.md‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,55 @@ var solveSudoku = function(board) {
439439
};
440440
```
441441

442+
### TypeScript
443+
444+
```typescript
445+
/**
446+
Do not return anything, modify board in-place instead.
447+
*/
448+
function isValid(col: number, row: number, val: string, board: string[][]): boolean {
449+
let n: number = board.length;
450+
// 列向检查
451+
for (let rowIndex = 0; rowIndex < n; rowIndex++) {
452+
if (board[rowIndex][col] === val) return false;
453+
}
454+
// 横向检查
455+
for (let colIndex = 0; colIndex < n; colIndex++) {
456+
if (board[row][colIndex] === val) return false;
457+
}
458+
// 九宫格检查
459+
const startX = Math.floor(col / 3) * 3;
460+
const startY = Math.floor(row / 3) * 3;
461+
for (let rowIndex = startY; rowIndex < startY + 3; rowIndex++) {
462+
for (let colIndex = startX; colIndex < startX + 3; colIndex++) {
463+
if (board[rowIndex][colIndex] === val) return false;
464+
}
465+
}
466+
return true;
467+
}
468+
function solveSudoku(board: string[][]): void {
469+
let n: number = 9;
470+
backTracking(n, board);
471+
function backTracking(n: number, board: string[][]): boolean {
472+
for (let row = 0; row < n; row++) {
473+
for (let col = 0; col < n; col++) {
474+
if (board[row][col] === '.') {
475+
for (let i = 1; i <= n; i++) {
476+
if (isValid(col, row, String(i), board)) {
477+
board[row][col] = String(i);
478+
if (backTracking(n, board) === true) return true;
479+
board[row][col] = '.';
480+
}
481+
}
482+
return false;
483+
}
484+
}
485+
}
486+
return true;
487+
}
488+
};
489+
```
490+
442491
### C
443492

444493
```C

0 commit comments

Comments
(0)

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