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. zombodb 几个方便的_cat api

    zombodb 暴露所有es _cat/ api 为视图,我们可以通过视图方便的查询es 的信息,默认在zdb的schema 中 包含的视图 几个方便的view 查看索引统计信息zdb.index_s ...

  2. linux删除某用户密码

    1.清空一个linux用户密码 # passwd -d user1 passwd: password expiry information changed. 2.指定key登录 ssh port111 ...

  3. dos2unix 批量转化文件

    在windows和linux双平台下开发,同时也用git作为同步工具,但前期没有注意,导致很多文件使用windows下的换行符CRLF 参考资料了解dos2unix可以转化格式. 但有个问题,虽然可以 ...

  4. Updates were rejected because the tip of your current branch is behind 问题出现解决方案

    提供如下几种方式: 1.使用强制push的方法(多人协作时不可取): git push -u origin master -f 2.push前先将远程repository修改pull下来 git pu ...

  5. PAT 甲级 1041 Be Unique (20 分)

    1041 Be Unique (20 分) Being unique is so important to people on Mars that even their lottery is desi ...

  6. OS&SYS&Shuti模块

    #sys.argv   主要针对脚本可以读取参数 Shuti模块 import shutil f1=open('笔记',encoding='utf-8') f2=open('笔记2','w',enco ...

  7. 刘志梅 201771010115 《面向对象程序设计(java)》 第八周学习总结

    实验六 接口的定义与使用 实验时间 2018-10-18 1.实验目的与要求 (1) 接口定义:接口不是类,而是对类的一组需求描述,这些类要遵从接口描述的统一格式进行定义:由常量和一组抽象方法组成:接 ...

  8. vlan划分

    1.vlan:虚拟局域网: 作用:划分广播域,抑制广播风暴: 2.vlan技术的优点: 有效控制广播域范围: 增强局域网的安全性: 灵活构建虚拟工作组: 3.vlan划分的方式: 基于端口: 基于MA ...

  9. java新手抖机灵(java新手技巧)

    java新手抖机灵(java新手技巧) 1.交换两个整数的值 好处是不用定义临时变量,显得代码简洁,提高运行效率 其实也可以用+-*/进行这种运算 比如可以这样: a = a + b; b = a - ...

  10. 解决spring-security session超时 Ajax 请求没有重定向的问题

    开始时, 代码是这样的: $.ajax({ type : "POST", url : sSource, cache : false, dataType : "json&q ...