[leetcode]37. Sudoku Solver 解数独
Write a program to solve a Sudoku puzzle by filling the empty cells.
A sudoku solution must satisfy all of the following rules:
- Each of the digits
1-9
must occur exactly once in each row. - Each of the digits
1-9
must occur exactly once in each column. - Each of the the digits
1-9
must occur exactly once in each of the 93x3
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 解数独的更多相关文章
- [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 36. Valid Sudoku 数独问题
三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Sudoku ...
- [LeetCode] Sudoku Solver 解数独,递归,回溯
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- LeetCode 37 Sudoku Solver(求解数独)
题目链接: https://leetcode.com/problems/sudoku-solver/?tab=Description Problem : 解决数独问题,给出一个二维数组,将这个数独 ...
- Java [leetcode 37]Sudoku Solver
题目描述: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated ...
- 037 Sudoku Solver 解数独
写一个程序通过填充空格来解决数独.空格用 '.' 表示. 详见:https://leetcode.com/problems/sudoku-solver/description/ class Solut ...
- [leetcode 37]sudoku solver
1 题目: 根据给出的数独,全部填出来 2 思路: 为了做出来,我自己人工做了一遍题目给的数独.思路是看要填的数字横.竖.子是否已经有1-9的数字,有就剔除一个,最后剩下一个的话,就填上.一遍一遍的循 ...
- leetcode 37 Sudoku Solver java
求数独,只要求做出一个答案就可以. 刚开始对题意理解错误,以为答案是唯一的, 所以做了很久并没有做出来,发现答案不唯一之后,使用回溯.(还是借鉴了一下别人) public class Solution ...
- [Leetcode][Python]37: Sudoku Solver
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 37: Sudoku Solverhttps://oj.leetcode.co ...
随机推荐
- openstry lua redis实现负载均衡
需求: 通过URI地址http://10.0.0.148/test2?uuid=123的uuid参数值的第一位,去实现redis的负载均衡 若uuid第一位为1,那么去10.0.0.148的redis ...
- Hadoop-HBASE 热添加新节点
Hadoop-HBASE 热添加新节点 环境:192.168.137.101 hd1192.168.137.102 hd2192.168.137.103 hd3192.168.137.104 hd4四 ...
- jsp中forward与redirect
一.调用方式 我们知道,在servlet中调用转发.重定向的语句如下: request.getRequestDispatcher("new.jsp").forward(reques ...
- Mysql临时文件目录控制
查看mysql的log-error日志发现如下错误: ERROR 3 (HY000): Error writing file '/tmp/MYbEd05t' (Errcode: 28) 这是由于mys ...
- springboot 中的commandLineRunners接口
首先看实现了两个接口运行的顺序结果: My1: package com.example.commandlinerunner; import lombok.extern.java.Log;import ...
- TCP/IP学习20180630-数据链路层-router choose
IP路由选择 当一个IP数据包准备好了的时候,IP数据包(或者说是路由器)是如何将数据包送到目的地的呢?它是怎么选择一个合适的路径来"送货"的呢? 最特殊的情况是目的主机和主机直连 ...
- 【OpenStack】network相关知识学习
network 类型 local:通信不跨主机,必须同一网段,主要做单机测试使用: flat:统计可以跨主机,但是需要在同一网段: 每个 flat network 都会独占一个物理网卡 计算节点上 b ...
- 对pytorch中Tensor的剖析
不是python层面Tensor的剖析,是C层面的剖析. 看pytorch下lib库中的TH好一阵子了,TH也是torch7下面的一个重要的库. 可以在torch的github上看到相关文档.看了半天 ...
- [UE4]Spacer
一.Spacer:留白占位控件 二.如下图所示,如果想要2个按钮都在容器右对齐: 三.可以放一个Spacer到最左边,设置成Fill,Spacer控件就是起到占位的作用.
- [UE4]Dynamic Entry Box
Dynamic Entry Box:条目创建容器 一个特殊的容器,能够自动创建条目,在可变数量条目的时候,但是又不值得创建一个ListView或者Tile View. 注意: Dynamic Entr ...