Sudoku is a number-placement puzzle: fill a 9×9 grid so that every row, every column, and each of the nine 3×3 boxes contains the digits 1 through 9 exactly once. You are handed a partially filled board — a 0 marks an empty cell, and the digits 1–9 are fixed clues — and must complete it. This is LeetCode 37; every puzzle you receive is guaranteed to have exactly one solution. See Sudoku for the rules and history.
sudokuSolver(board) // board: number[][], a 9x9 grid; 0 = empty, 1..9 = clues
// fills the board IN PLACE and returns the same board
You mutate the grid you are given and return that same reference — not a fresh copy.
// The board is a 9x9 array of numbers. A 0 is an empty cell to fill.
const board = [
[5, 3, 0, 0, 7, 0, 0, 0, 0],
[6, 0, 0, 1, 9, 5, 0, 0, 0],
[0, 9, 8, 0, 0, 0, 0, 6, 0],
[8, 0, 0, 0, 6, 0, 0, 0, 3],
[4, 0, 0, 8, 0, 3, 0, 0, 1],
[7, 0, 0, 0, 2, 0, 0, 0, 6],
[0, 6, 0, 0, 0, 0, 2, 8, 0],
[0, 0, 0, 4, 1, 9, 0, 0, 5],
[0, 0, 0, 0, 8, 0, 0, 7, 9],
];
const solved = sudokuSolver(board);
solved === board; // true — the same array, now filled in
// solved is:
// [
// [5, 3, 4, 6, 7, 8, 9, 1, 2],
// [6, 7, 2, 1, 9, 5, 3, 4, 8],
// [1, 9, 8, 3, 4, 2, 5, 6, 7],
// [8, 5, 9, 7, 6, 1, 4, 2, 3],
// [4, 2, 6, 8, 5, 3, 7, 9, 1],
// [7, 1, 3, 9, 2, 4, 8, 5, 6],
// [9, 6, 1, 5, 3, 7, 2, 8, 4],
// [2, 8, 7, 4, 1, 9, 6, 3, 5],
// [3, 4, 5, 2, 8, 6, 1, 7, 9],
// ]
(r, c) has its top-left corner at row 3 * Math.floor(r / 3) and column 3 * Math.floor(c / 3); its nine cells span that corner plus the next two rows and columns.1–9, with 0 for empty. You do not need to validate the input or handle any grid shape other than 9×9.Unlock the solution & editor
Runnable editor + tests
Solve in the browser with instant Jest feedback.
Detailed solutions
Walkthroughs, edge cases, and complexity notes.
Multi-framework variants
React, Vue, Vanilla, Angular — same question, different stacks.