LeetCode 36 Sudoku Solver
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.
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的更多相关文章
- Leetcode 笔记 36 - Sudoku Solver
题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...
- leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题
三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Sudoku ...
- 【leetcode】Sudoku Solver
Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...
- LeetCode 037 Sudoku Solver
题目要求:Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells ...
- [LeetCode] 37. Sudoku Solver 求解数独
Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy ...
- 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 ...
- Java [leetcode 37]Sudoku Solver
题目描述: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated ...
- [leetcode]37. Sudoku Solver 解数独
Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy ...
- LeetCode 37 Sudoku Solver(求解数独)
题目链接: https://leetcode.com/problems/sudoku-solver/?tab=Description Problem : 解决数独问题,给出一个二维数组,将这个数独 ...
随机推荐
- 怎样推断DIV中的内容为空
怎样推断DIV中的内容为空 1.问题背景 推断div内部是否为空.假设为空,给出无数据提示:否则显示正常页面 2.设计源代码 <!DOCTYPE html PUBLIC "-//W3C ...
- [TypeScript] Understand lookup types in TypeScript
Lookup types, introduced in TypeScript 2.1, allow us to dynamically create types based on the proper ...
- Cygwin 与 MinGW/MSYS/MSYS2,如何选择?甚至还有GNU utilities for Win32
Cygwin与MinGW/MSYS,如何选择? 2012-11-03更新:加入 MSYS 的内容. 2013-10-15更新:修改表格格式,加入介绍链接. 2014-12-17更新:加入 MSYS2 ...
- bash - Logical_OR
转载 https://bash.cyberciti.biz/guide/Logical_OR Logical OR ← Logical AND Home Logical Not ! → Logic ...
- Xcode6 模拟器路径
Xcode6公布后,出现了非常多的变动,功能性的变动,在这里不进行过多的赘述,在WWDC上苹果已经进行了讲述,网上也有非常多文章,这里要介绍的是一些不太easy发现的,但非常重要的小地方. ...
- js字符串转换为数字的三种方法。(转换函数)(强制类型转换)(利用js变量弱类型转换)
js字符串转换为数字的三种方法.(转换函数)(强制类型转换)(利用js变量弱类型转换) 一.总结 js字符串转换为数字的三种方法(parseInt("1234blue"))(Num ...
- ArcGIS网络概述
转载自原文 ArcGIS网络概述 一.地理网络 (一)基本概念 由一系列相互连通的点和线组成,用来描述地理要素(资源)的流动情况. (二)网络类型 1.定向网络 (1)流向由源(source)至汇(s ...
- [Ramda] Eliminate Function Arguments (Point-Free Style) with Ramda's Converge
When doing comparisons inside of functions, you end of relying heavily on the argument passed into t ...
- Android自定义控件View(二)继承控件
在前一篇博客中学习了Android自定义控件View的流程步骤和注意点,不了解的童鞋可以参考Android自定义控件View(一).这一节开始学习自定义控件View(二)之继承系统已有的控件.我们来自 ...
- 数据序列化之protobuf
数据序列化之protobuf 很多时候需要将一些数据打包,就是把这些数据搞在一起,方便处理.最常见的情况就是把需要传输的数据,当然数据不止一条,打包成一个消息,然后发送出去,接收端再以一定的规则接收并 ...