【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 ...
随机推荐
- Csharp多态的实现(抽象类)
1.什么是抽象类 抽象类是虚拟的类,不能创建对象,用abstract修饰,在子类中用override进行重写 抽象类中可以存放抽象方法,属性,也可以存放非抽象方法,属性(这个在下面的代码可以看出来的) ...
- 漫谈AOP开发之初探AOP及AspectJ的用法
一.为什么需要AOP技术 AOP 是一个很成熟的技术. 假如项目中有方法A.方法B.方法C……等多个方法, 如果项目需要为方法A.方法B.方法C……这批方法增加具有通用性质的横切处理. 下图可以形 ...
- EC读书笔记系列之13:条款25 考虑写出一个不抛异常的swap函数
记住: ★当std::swap对你的类型效率不高时,提供一个swap成员函数,并确定其不抛出异常 ★若你提供一个member swap,也该提供一个non-member swap来调用前者.对于cla ...
- Mysql表锁定解决
#查看进程SELECT *FROM information_schema.processlistWHERE USER = 'root' AND state LIKE 'Waiting%';#杀掉进程K ...
- C#BASE64 UTF8字符串加密解密
base 64 解码 base64 bb = new base64(); string orgStr= Encoding.Default.GetString(bb.GetDecoded("b ...
- python之字符串格式化(format)
用法: 它通过{}和:来代替传统%方式 1.使用位置参数 要点:从以下例子可以看出位置参数不受顺序约束,且可以为{},只要format里有相对应的参数值即可,参数索引从0开,传入位置参数列表可用*列表 ...
- GetMemory()函数
NO1 void GetMemory(char *p) { p=(char *)malloc(100); } void Test() { char * str=NULL; GetMemory(str) ...
- oracle 查询表的大小,表空间的使用情况,默认表空间
oracle 查询表的大小,表空间的使用情况,默认表空间 oracle 查询表的大小,表空间的使用情况,默认表空间 --查看某张表占用磁盘空间大小 ( 表名大写 ) Select Segment_Na ...
- JS---DOM概述
DOM DOM:文档对象模型document object model DOM三层模型: DOM1:将HTML文档封装成对象 DOM2:将XML文档封装成对象 DOM3:将XML文档封装成对象 DOM ...
- Oracle EBS-SQL (AR-1):检查应收收款发生额
SELECT SUM(nvl(dist.amount_dr,0))-SUM(nvl(dist.amount_cr,0)) FROM ar_cash_receipt_history_all hist, ...