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

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

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. 关于HashMap中的负载因子

    这两天在看HashMap的时候,被负载因子float loadFactor搞得很晕,经过一天的研究,最后理出了自己的一点个人见解. 在HashMap的底层存在着一个名字为table的Entry数组,在 ...

  2. Linux摄像头驱动学习之:(一)V4L2_框架分析

    这段时间开始搞安卓camera底层驱动了,把以前的的Linux视频驱动回顾一下,本篇主要概述一下vfl2(video for linux 2). 一. V4L2框架: video for linux ...

  3. 解决maven Generating project in Interactive mode卡死问题(转)

    原文链接:http://blog.csdn.net/only_wan/article/details/52975760 mvn 创建时在generating project in interactiv ...

  4. 给伪类设置z-index= -1;

    .column{ position: relative; float: left; padding: 30px 0; width: 25%; z-index: 0; background-color: ...

  5. 2013年9月份第1周51Aspx源码发布详情

    大型B2B家具门户网源码  2013-9-6 [VS2008]功能描述: 1.门户信息管理 安全取数据即使数据库连接中断不会报错 2.稳定性 每句代码经过3次以上检查.此网站还在运营3年了,没有出过问 ...

  6. [TOP10]十大渗透测试演练系统

    本文总结了目前网络上比较流行的渗透测试演练系统,这些系统里面都提供了一些实际的安全漏洞,排名不分先后,各位安全测试人员可以亲身实践如何利用这个漏洞,同时也可以学习到漏洞的相关知识. DVWA (Dam ...

  7. UNIX 网络编程第三版

    第五章p102: ps -t  pts/6 -o pid,ppid,tty,stat,args,wchan 在我的系统上运行时出现:TTY not found linux发行版为mint17.1 改用 ...

  8. # 20145210 《Java程序设计》第05周学习总结

    教材学习内容总结 第八章 异常处理 8.1语法与继承架构 •使用 try.catch •Java中所有信息都会被打包为对象,如果愿意,可以尝试(try)捕捉(catch)代表错误的对象后做一些处理 • ...

  9. PHP CI框架email类发送邮件

    用CI框架发送邮件类 在中文标题太长的情况下会出现乱码,搜索后说是发送邮件的时候有标题长度的限制,按说的方法修改后,还是没能得到解决,后来发现需要转换邮件标题的编码,解决方法如下: 打开 librar ...

  10. HDU 4986

    http://acm.hdu.edu.cn/showproblem.php?pid=4986 题意:n个钥匙放在n个箱子里,每个钥匙和箱子一一对应,求打开所有箱子的期望 题解: 题意: 求随机排列的期 ...