【LeetCode题意分析&解答】37. 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.
题意分析:
本题是要解决一个数独问题。输入一个部分数独,将空白的格子填上正确的数字,只需要找到一个正确的解即可。
解答:
关于数独的规则,在上一篇文章里面有讲【LeetCode题意分析&解答】36. Valid Sudoku,这里不再赘述。
填入的数字是在1~9之间,我们先将当前节点置为某个要填入的数,并判断是否符合数独的规则。如果符合则继续循环,如果不符合则退回来填入其他的数。如果按照这个思路去做,就是一个典型的深度优先搜索的回溯算法。
AC代码:
class Solution(object):
def solveSudoku(self, board):
def backtracing():
for i in xrange(9):
for j in xrange(9):
if board[i][j] != '.':
continue
for v in [str(c) for c in xrange(1, 10)]:
# judge if against to the rule
if v in board[i] or v in [board[k][j] for k in xrange(9)] or v in \
[board[i / 3 * 3 + m][j / 3 * 3 + n] for m in xrange(3) for n in xrange(3)]:
continue
board[i][j] = v
if backtracing():
return True
else:
board[i][j] = '.'
return False
return True
backtracing()
【LeetCode题意分析&解答】37. Sudoku Solver的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- 【LeetCode题意分析&解答】36. Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
- 【LeetCode题意分析&解答】38. Count and Say
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...
- 【LeetCode题意分析&解答】43. Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- 【LeetCode题意分析&解答】42. Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- 【LeetCode题意分析&解答】41. First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- 【LeetCode题意分析&解答】39. Combination Sum
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- 【LeetCode题意分析&解答】34. Search for a Range
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
随机推荐
- bootstrap注意事项(二)
1.内联子标题 在标题内还可以包含 <small> 标签或赋予 .small 类的元素,可以用来标记副标题. <!DOCTYPE html> <html> < ...
- SQLSERVER分布式事务使用实例
实例一 尊重原著作:本文参考自http://www.jb51.net/article/43540.htm --BEGIN DISTRIBUTED TRANSACTION [transactionnam ...
- 摩根斯坦利 - 2016年09月8日 面试题 - HashMap
摩根斯坦利 - 2016年09月8日 面试题: 给定一个 Map<Person, Object> map = new HashMap<Person, Object>(); 放入 ...
- poj2891--扩展欧几里德算法
/* 该题使用的是扩展欧几里德算法,求模线性同余方程: 分析题目:以题目输出结果为例 ,要求得到一个整数X可以满足 X % a = r,a,r,为数组名: 设数组元素为两个时, 列出方程:X % a1 ...
- Python核心编程读笔 8: 文件和输入输出
第九章 文件和输入输出 一.文件内建函数.方法.属性 1 文件内建函数 file_object = open(file_name, access_mode='r', buffering=-1) 工厂函 ...
- JavaWeb核心编程之(三.5)HTTP请求和接受表单数据
HTTP简介WEB浏览器与WEB拂去其之间的一问一答的交互过程, 必须遵循一定的规则,这个规则就是HTTP协议HTTP是hypertext transfer protocol(超文本传输协议)的简写, ...
- linux使用工具记录
linux工具查询手册: http://linuxtools-rst.readthedocs.io/zh_CN/latest/index.html
- c 函数传入数组。
php 里面 直接 count($arr); 一个函数搞定, c里面想判断下数组的个数却非常困难. 想到php是C写的,那看看他的函数怎么写的不就行了... 哦,天啊,,事实比我想的要复杂的多... ...
- Linux下diff使用简介
diff用来比较两个文件的差异.首先构建两个相似的文件. Hello文件 world文件 使用diff -u hello world > diff.txt,将两个文件的比对结果输入到diff. ...
- HBuilder的几个常用快捷键
Alt + [ 匹配括号 Alt + ↓跳转到下一个可编辑区 Ctrl + Alt + j 合并下一行 Ctrl + Alt + ←选择助手 Shift + 回车 Shift + 空格 Ctrl ...