Write a program to solve a Sudoku puzzle by filling the empty cells.

A sudoku solution must satisfy all of the following rules:

  1. Each of the digits 1-9 must occur exactly once in each row.
  2. Each of the digits 1-9 must occur exactly once in each column.
  3. Each of the the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid.

Empty cells are indicated by the character '.'.

A sudoku puzzle...

...and its solution numbers marked in red.

Note:

  • The given board contain only digits 1-9 and the character '.'.
  • You may assume that the given Sudoku puzzle will have a single unique solution.
  • The given board size is always 9x9.

题意:

解数独

Solution1: Backtracking

code

 class Solution {
public void solveSudoku(char[][] board) {
if (board == null || board.length != 9 || board[0].length != 9) return;
boolean tmp = helper(board, 0, 0);
} public boolean helper(char[][] board, int row, int col) {
if (board == null || board.length != 9 || board[0].length != 9) return false; while (row < 9 && col < 9) {
if (board[row][col] == '.') break; // find the 1st empty spot
if (col == 8) {// 说明要换行了
col = 0;
row++;
} else {
col++;
}
}
if (row >= 9) return true;
int nextRow = row + col / 8;
int nextCol = (col + 1) % 9; for (int num = 1; num <= 9; num++) {
if (isValid(board, row, col, num)) { // 该num在行,列,box都没出现过
board[row][col] = (char) (num + '0'); // 试着将num填入cur spot
boolean result = helper(board, nextRow, nextCol); // 递归调用,下一个
if (result) return true;
board[row][col] = '.'; // 如果num在cur spot不合法,回溯。 还原。
}
}
return false;
} // check num is valid at such spot
// which means in cur row, col or box, this num hasn't appeared before
public boolean isValid(char[][] board, int row, int col, int num) {
// check row
for (int i = 0; i < 9; i++) {
if (board[row][i] == num + '0')
return false;
}
//check col
for (int i = 0; i < 9; i++) {
if (board[i][col] == num + '0')
return false;
}
//check box
int rowoff = (row / 3) * 3;
int coloff = (col / 3) * 3;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[rowoff + i][coloff + j] == num + '0') return false;
}
}
return true;
}
}

[leetcode]37. Sudoku Solver 解数独的更多相关文章

  1. [LeetCode] 37. Sudoku Solver 求解数独

    Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy  ...

  2. leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题

    三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Sudoku ...

  3. [LeetCode] Sudoku Solver 解数独,递归,回溯

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  4. LeetCode 37 Sudoku Solver(求解数独)

    题目链接: https://leetcode.com/problems/sudoku-solver/?tab=Description   Problem : 解决数独问题,给出一个二维数组,将这个数独 ...

  5. Java [leetcode 37]Sudoku Solver

    题目描述: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated ...

  6. 037 Sudoku Solver 解数独

    写一个程序通过填充空格来解决数独.空格用 '.' 表示. 详见:https://leetcode.com/problems/sudoku-solver/description/ class Solut ...

  7. [leetcode 37]sudoku solver

    1 题目: 根据给出的数独,全部填出来 2 思路: 为了做出来,我自己人工做了一遍题目给的数独.思路是看要填的数字横.竖.子是否已经有1-9的数字,有就剔除一个,最后剩下一个的话,就填上.一遍一遍的循 ...

  8. leetcode 37 Sudoku Solver java

    求数独,只要求做出一个答案就可以. 刚开始对题意理解错误,以为答案是唯一的, 所以做了很久并没有做出来,发现答案不唯一之后,使用回溯.(还是借鉴了一下别人) public class Solution ...

  9. [Leetcode][Python]37: Sudoku Solver

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 37: Sudoku Solverhttps://oj.leetcode.co ...

随机推荐

  1. PythonStudy——字典的定义 Dictionary definition

    # 空字典 d1 = {} d2 = dict() # 用map映射创建字典 d3 = dict({'a': 1, 'b': 1}) print(d3) # 用关键字赋值方式 d4 = dict(na ...

  2. windows server 2008 R2之tomcat开机自启

    方法一: 写一个批处理文件autostartup.bat用来启动tomcat,内容如下.复制时不要把复制内容也复制进去 set CATALINA_HOME=C:\apache-tomcat-8.5.3 ...

  3. MySQL Execution Plan--EXPLAIN用法

    MySQL Explain新用法: --使用EXPLAIN来查看语句的最终执行计划 语法:EXPLAIN [EXTENDED] SELECT select_options --在MYSQL .7版本后 ...

  4. 1.4 安装Linux系统

    按F2进入BIOS,设置通过[CD/ROM]启动,如果是真实计算机,安装完后还需要重新设置为[硬盘启动] 设置分区如下图所示:

  5. 2.2 如何在Visio中写上、下角标

    快捷键:下标[“Ctrl”+ “=”] 上标[“Ctrl”+“shift”+“=”]

  6. 自己动手,丰衣足食!Python3网络爬虫实战案例

    本教程是崔大大的爬虫实战教程的笔记:网易云课堂 Python3+Pip环境配置 Windows下安装Python: http://www.cnblogs.com/0bug/p/8228378.html ...

  7. select函数总结

    阻塞方式block,就是进程或是线程执行到这些函数时必须等待某个事件的发生,如果事件没有发生,进程或线程就被阻塞,函数不能立即返回.使用Select就可以完成非阻塞non-block,就是进程或线程执 ...

  8. docker 在windows7 、8下的安装

    这里说明一下这种安装方式适合win7 win8的系统环境下安装的,当然win10也可以,但是win10有更好的方式 即安装Docker Toolbox,同时还附加安装 Docker Client fo ...

  9. PHP运行出现Notice : Use of undefined constant 的解决办法

    这些是 PHP 的提示而非报错,PHP 本身不需要事先声明变量即可直接使用,但是对未声明变量会有提示.一般作为正式的网站会把提示关掉的,甚至连错误信息也被关掉 关闭 PHP 提示的方法 搜索php.i ...

  10. LINUX PID 1 和 SYSTEMD

    要说清 Systemd,得先从Linux操作系统的启动说起.Linux 操作系统的启动首先从 BIOS 开始,然后由 Boot Loader 载入内核,并初始化内核.内核初始化的最后一步就是启动 in ...