求数独,只要求做出一个答案就可以。

刚开始对题意理解错误,以为答案是唯一的, 所以做了很久并没有做出来,发现答案不唯一之后,使用回溯。(还是借鉴了一下别人)

public class Solution {
public void solveSudoku(char[][] board) {
HashSet[] hashset = new HashSet[27];
for (int i = 0; i < 27; i++)
hashset[i] = new HashSet<Character>();
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
char Char = board[i][j];
if (Char != '.') {
hashset[i].add(Char);
hashset[9 + j].add(Char);
hashset[18 + (i / 3) * 3 + j / 3].add(Char);
}
}
}
int flag = 0;
char[][][] num = null ;
while ( flag == 0) {
flag = 1;
num = new char[9][9][9];
for (int i = 0; i < 9; i++) {// i代表第i个hashset
for (int j = 1; j < 10; j++) {// j代表1-9
char ch = (char) (j + '0');
int[] test = new int[2];
if (!hashset[i].contains(ch)) {
test[0] = 0;
for (int k = 0; k < 9; k++) {
char Ch = board[i][k];
if (Ch == '.') {
if (!hashset[9 + k].contains(ch) && !hashset[18 + (i / 3) * 3 + k / 3].contains(ch)) {
addNum(num, i, k, ch);
test[0]++;
test[1] = k;
}
}
}
}
if (test[0] == 1) {
board[i][test[1]] = ch;
hashset[i].add(ch);
flag = 0;
hashset[9 + test[1]].add(ch);
hashset[18 + (i / 3) * 3 + test[1] / 3].add(ch);
} }
} for (int qq = 0; qq < 9 && flag == 1; qq++) {
for (int j = 0; j < 9 && flag == 1; j++) {
if (getlen(num[qq][j]) == 1) {
char ch = num[qq][j][0];
board[qq][j] = ch;
flag = 0;
hashset[qq].add(ch);
hashset[9 + j].add(ch);
hashset[18 + (qq / 3) * 3 + j / 3].add(ch);
} }
} }
solve(board);
} public boolean solve(char[][] board){
for(int i = 0; i < board.length; i++){
for(int j = 0; j < board[0].length; j++){
if(board[i][j] == '.'){
for(char c = '1'; c <= '9'; c++){//trial. Try 1 through 9 for each cell
if(isValid(board, i, j, c)){
board[i][j] = c; //Put c for this cell if(solve(board))
return true; //If it's the solution return true
else
board[i][j] = '.'; //Otherwise go back
}
}
return false;
}
}
}
return true;
} public boolean isValid(char[][] board, int i, int j, char c){
for(int row = 0; row < 9; row++)
if(board[row][j] == c)
return false; for(int col = 0; col < 9; col++)
if(board[i][col] == c)
return false; for(int row = (i / 3) * 3; row < (i / 3) * 3 + 3; row++)
for(int col = (j / 3) * 3; col < (j / 3) * 3 + 3; col++)
if(board[row][col] == c)
return false;
return true;
}
public static int getlen(char[] num) {
int len = 0;
for (int i = 0; i < 9; i++) {
if (num[i] < '1' || num[i] > '9') {
return len;
} else
len++;
}
return len;
} public static void addNum(char[][][] num, int num1, int num2, char ch) {
for (int i = 0; i < 9; i++) {
if (num[num1][num2][i] < '0' || num[num1][num2][i] > '9') {
num[num1][num2][i] = ch;
break;
} } }
}

回溯法还是比较简单的,就是在实现的时候,如果想要提高运行的速度和空间,那么需要费一些心思来考虑。

附上借鉴的代码

public class Solution {
public void solveSudoku(char[][] board) {
if(board == null || board.length == 0)
return;
solve(board);
} public boolean solve(char[][] board){
for(int i = 0; i < board.length; i++){
for(int j = 0; j < board[0].length; j++){
if(board[i][j] == '.'){
for(char c = '1'; c <= '9'; c++){//trial. Try 1 through 9 for each cell
if(isValid(board, i, j, c)){
board[i][j] = c; //Put c for this cell if(solve(board))
return true; //If it's the solution return true
else
board[i][j] = '.'; //Otherwise go back
}
}
return false;
}
}
}
return true;
} public boolean isValid(char[][] board, int i, int j, char c){
//Check colum
for(int row = 0; row < 9; row++)
if(board[row][j] == c)
return false; //Check row
for(int col = 0; col < 9; col++)
if(board[i][col] == c)
return false; //Check 3 x 3 block
for(int row = (i / 3) * 3; row < (i / 3) * 3 + 3; row++)
for(int col = (j / 3) * 3; col < (j / 3) * 3 + 3; col++)
if(board[row][col] == c)
return false;
return true;
}
}

leetcode 37 Sudoku Solver java的更多相关文章

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

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

  2. Java [leetcode 37]Sudoku Solver

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

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

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

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

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

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

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

  6. [leetcode 37]sudoku solver

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

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

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

  8. 【LeetCode】37. Sudoku Solver

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

  9. 【leetcode】Sudoku Solver

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

随机推荐

  1. WP8 学习 在APP.XAML中加入Resources

    <Application.Resources> <local:LocalizedStrings xmlns:local="clr-namespace:test1" ...

  2. No module ata_piix found的解决方法

    在一台as4u6的机器上升级内核到2.6.18时,最好make install的时候报了一个WARNING: No module ata_piix found for 2.6.18, 开始没有在意,重 ...

  3. Android的R.java文件

    1.Android资源管理简介: Android应用程序资源可以分为两大类,分别放在assets和res文件夹下.assets目录下保存的是一些原始的文件,可以以任何方式来进行组织.这些文件最终会被原 ...

  4. jQuery Ajax之load()方法

    jQuery对Ajax操作进行了封装,在jQuery中$.ajax()方法属于最底层的方法,第2层是load().$.get()和$.post()方法,第3层是$.getScript()和$.getJ ...

  5. 实现viewpager下的圆点滑动

    在Drawable目录下创建资源文件 使用shape标签画出背景圆点与当前圆点 背景圆点: <?xml version="1.0" encoding="utf-8& ...

  6. IIS 发布后文件拒绝访问

    今天遇到一个很小的问题,代码中写XML文件,本地运行没有问题,一发布到服务器上就出现 代码如下: XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load ...

  7. Ubuntu 14.10 下awk命令详解

    简介 awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再 ...

  8. Varnost slovenskih GSM omrežij III

    V torek smo pisali tudi o tem, da Si.Mobil v svojem omrežju dovoli uporabo A5/0 (nešifriranega preno ...

  9. 考古备份:a.out文件ELF文件头中魔数的由来

    来源: <程序员的自我修养>3.4节. 补充: http://wiki.osdev.org/ELF http://www.linux-mag.com/id/116/ http://en.w ...

  10. js 检测 flash插件以及版本号 通用所有浏览器

    var fls = flashChecker(); if (fls.h) { if (fls.v < parseFloat('8.0')) { alert("您当前的flash pla ...