leetcode Sudoku Solver python
#the define of Sudoku is on this link : http://sudoku.com.au/TheRules.aspx
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.
class Solution(object):
def solveSudoku(self, board):
"""
:type board: List[List[str]]
:rtype: void Do not return anything, modify board in-place instead.
"""
def isValid(x,y):
tmp=board[x][y]
board[x][y]='D'
for i in range(9):
if board[i][y] == tmp:
return False
for i in range(9):
if board[x][i] == tmp:
return False
for i in range(3):
for j in range(3):
if board[(x/3)*3+i][(y/3)*3+j] == tmp:
return False
board[x][y]=tmp
return True
def dfs(board):
for i in range(9):
for j in range(9):
if board[i][j] == '.':
for k in '':
board[i][j] = k
if isValid(i,j) and dfs(board):
return True
board[i][j] = '.'
return False
return True
dfs(board)
leetcode Sudoku Solver python的更多相关文章
- [LeetCode] Sudoku Solver 求解数独
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- Leetcode: Sudoku Solver
July 19, 2015 Problem statement: Write a program to solve a Sudoku puzzle by filling the empty cells ...
- LEETCODE —— Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- [LeetCode] Sudoku Solver(迭代)
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- leetcode—sudoku solver
1.题目描述 Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicate ...
- [LeetCode] Sudoku Solver 解数独,递归,回溯
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- [Leetcode][Python]37: Sudoku Solver
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 37: Sudoku Solverhttps://oj.leetcode.co ...
- Leetcode 笔记 36 - Sudoku Solver
题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...
- leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题
三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Sudoku ...
随机推荐
- Fix Elementary Boot Screen (plymouth) After Installing Nvidia Drivers
Q:I just installed propietary nvidia drivers, after that the glowing “e” plymouth theme was gone, on ...
- idea git 注意事项
1.使用idea进行不同分支切换的时候,一定要先将代码当前分支的代码提交(可以不push),否则当前分支未提交的代码被认为即将切换的代码,在切换过程中会产生冲突. 2.使用idea进行merge的时候 ...
- IDE idea 更换项目的JDK步骤
1.如图:
- C#运算符的优先级
在C#中,一共有38个常用的运用符,根据它们所执行运算的特点和它们的优先级,为了便于记忆,我将它们归为七个等级:1.单元运算符和括号.2.常规算术运算符.3.位移运算符.4.比较运算符.5.逻辑运算符 ...
- 关于Get和Post的学习笔记(五)
一 .Get和Post的优缺点 1. Get> 优点: 编写起来比较简单,只要在地址后面拼接数据即可. > 缺点: 提交的数据有长度限制.安全性比较差 2.Post > 优点: 提交 ...
- CentOS mini版安装后增加gcc编译环境
使用如下命令即可: sudo yum install gcc gcc-c++ make -y
- CSS3实现漂亮ToolTips
演示Demo 5种效果的主要css如下: /* transform:向元素应用2D或3D转换; transform-origin属性只有在定义了transform以后才生效 */ .tooltip-e ...
- Spring-----事务配置的五种方式
转载自:http://blog.csdn.net/hekewangzi/article/details/51712821
- .Net之托管堆资源分配
托管堆分配资源: 一:进程初始化是,CLR要保留一块联系的地址空间,这个地址空间最初并没有对应的物理存储空间.这个地址空间就是托管堆.托管堆还维护着一个指针,我把它称为NextObjPtr.它指向下个 ...
- Landsat8数据不同波段组合的用途
2013年2月11日发射的Landsat系列最新卫星Landsat8,携带有OLI陆地成像仪和TIRS热红外传感器,Landsat8的OLI陆地成像仪包括9个波段,OLI包括了ETM+传感器所有的波段 ...