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

Empty cells are indicated by the character '.'.

You may assume that there will be only one unique solution.

A sudoku puzzle...

...and its solution numbers marked in red.

思路1:使用暴力DFS
public class Solution {
private boolean isValidSudoku(char[][] board, int row, int column) {
int i, j;
int[] valid1 = new int[10];
int[] valid2 = new int[10];
int[] valid3 = new int[10];
for (i = 0; i <9; i++) {
if (board[i][column] != '.') {
if (valid1[board[i][column] - '0'] > 0)
return false;
valid1[board[i][column] - '0']++;
}
if (board[row][i] != '.') {
if (valid2[board[row][i] - '0'] > 0)
return false;
valid2[board[row][i] - '0']++;
}
}
for (i = (row / 3) * 3; i < (row / 3 + 1) * 3; i++) {
for (j = (column / 3) * 3; j < (column / 3 + 1) * 3; j++) {
if (board[i][j] != '.') {
if (valid3[board[i][j] - '0'] > 0)
return false;
valid3[board[i][j] - '0']++;
}
}
}
return true;
} private boolean internalSolveSudoku(char[][] board) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (board[i][j] == '.') {
for (int k = 1; k <= 9; k++) {
board[i][j] = (char) ('0' + k);
if (isValidSudoku(board, i, j)) {
if (internalSolveSudoku(board)) {
return true;
}
}
board[i][j] = '.';
return false;
}
} }
}
return true;
} public void solveSudoku(char[][] board) {
internalSolveSudoku(board);
}
}

思路2:使用Dancing Links,代码后补。


版权声明:本文博客原创文章。博客,未经同意,不得转载。

LeetCode 36 Sudoku Solver的更多相关文章

  1. Leetcode 笔记 36 - Sudoku Solver

    题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...

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

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

  3. 【leetcode】Sudoku Solver

    Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...

  4. LeetCode 037 Sudoku Solver

    题目要求:Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells ...

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

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

  6. Java for LeetCode 037 Sudoku Solver

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

  7. Java [leetcode 37]Sudoku Solver

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

  8. [leetcode]37. Sudoku Solver 解数独

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

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

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

随机推荐

  1. 【7.89%】【BNUOJ 52303】Floyd-Warshall

    Time limit: 2 seconds Memory limit: 1024 megabytes In ICPCCamp, there are n cities and m (bidirectio ...

  2. Docker容器应用日志查看

    原文:Docker容器应用日志查看 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/benben_2015/article/details/80708 ...

  3. PatentTips - Sprite Graphics Rendering System

    BACKGROUND This disclosure relates generally to the field of computer graphics. More particularly, b ...

  4. Drupal 7 模块开发 建立模块帮助信息(hook_help)

    建立模块请參考 <Drupal 7 模块开发 建立> 假设你要支持中文,文件格式必须保存为 UTF-8.NO BOM ------------------------------ hook ...

  5. Android onKeyDown监听返回键无效

    当我们的Activity继承了TabActivity,在该类中重写onKeyDown是监听不到返回键的, 具体解决方法如下: 重写dispatchKeyEvent /** * 退出 */ @Overr ...

  6. python 设计模式之 单例模式

    单例模式是做为"全局变量"的替代品出现的.所以它具有全局变量的特点:全局可见.贯穿应用程序的整个生命期,保证在程序执行中,某个类仅仅存在一个实例,所以通常不希望类中的构造函数被调用 ...

  7. Redis的增删改查、持久化你会了吗

    原文:Redis的增删改查.持久化你会了吗 Redis是用C语言实现的,一般来说C语言实现的程序"距离"操作系统更近,执行速度相对会更快. Redis使用了单线程架构,预防了多线程 ...

  8. js中 慎用for(var o in arrays) 遍历数组,for(var i,i< objects.length;i++)与for(var i,n = objects.length;i<n;i++) 的性能区别

    原文:js中 慎用for(var o in arrays) 遍历数组,for(var i,i< objects.length;i++)与for(var i,n = objects.length; ...

  9. JDBC连接数据库步骤及Class.forName()(转)

    JDBC连接数据库 JDBC是Sun公司制定的一个可以用Java语言连接数据库的技术. 一.JDBC基础知识 JDBC(Java DataBase Connectivity,java数据库连接)是一种 ...

  10. UIPasteboard粘贴板:UIlabel开启复制粘贴功能(一)

    首先,因为苹果只放出来了 UITextView,UITextField,webView三个控件的剪贴板,所以一般控件的剪贴板都被禁用了,因此,我们首先要做的就是把这属性放出来,其实就是实现三个简单的方 ...