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. PythonStudy1——Python 值拷贝 浅拷贝 深拷贝

    拷贝:对值进行复制的过程 # 值拷贝:应用场景最多  ls = [1, 'abc', [10]] ls1 = ls # ls1直接将ls中存放的地址拿过来  # ls内部的值发生任何变化,ls1都会随 ...

  2. dubbo 基础入门

    一.什么是dubbo? Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案,说白了就是个远程服务调用的分布式框架. dubbo产生的背景 ① 单一 ...

  3. grep init 与 grep [i]nit

    看grep的知识点的时候,在XXX博客里看到一个这样的例子,一直在纠结,纠结,init与[i]nit 匹配到的东西不应该时一样的嘛,为什么一个匹配得出来,一个不行.后来在群里问了某位大哥,耐心的讲解, ...

  4. 如何在myeclipse中安装spket插件

    在web开发中,经常会遇到自动提示,比如jquery.extjs等,在myeclipse写这些代码时需要自动提示,就需要安装spket插件,具体方法见下面 工具/原料   myeclipse spke ...

  5. Syncthing – 数据同步利器---自己的网盘,详细安装配置指南,内网使用,发现服务器配置

    简介: 无论办公.文件共享.团队协作还是家庭照片.视频.音乐.高清电影的存储,我们常常都有文件同步和存储的需求.但随着国内各大网盘的花式阵亡或限速,早已没什么好选择了.好吧,我已经转战使用onedri ...

  6. Ubuntu更新时提示错误 E: Sub-process /usr/bin/dpkg returned an error code (1)

    $ sudo su //root权限 $ sudo mv /var/lib/dpkg/info /var/lib/dpkg/info_old //现将info文件夹更名 $ sudo mkdir /v ...

  7. 过滤器手动注入Service Bean方法

    @Override public void init(FilterConfig arg0) throws ServletException { ServletContext servletContex ...

  8. 架构-架构风格:REST

    ylbtech-架构-架构风格:REST REST即表述性状态传递(英文:Representational State Transfer,简称REST)是Roy Fielding博士在2000年他的博 ...

  9. [UE4]Scroll Box带滚动条的容器

    一.黑边,当可以往下滚动的时候,下边会出现黑边.当可以往上滚动的时候,上边也会出现黑边.   Scroll Box.Style.Style:也可以自定义上下左右黑边的样式: 二.Scroll Box. ...

  10. (转)适用微信小程序的table表格(带隔行变色)

    原文地址 table.wxml <view class="table"> <view class="tr bg-w"> <view ...