【LeetCode题意分析&解答】36. Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.
The Sudoku board could be partially filled, where empty cells are filled with the character '.'
.
A partially filled sudoku which is valid.
Note:
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
题意分析:
本题是验证一个数独(不一定是完整的,空白的元素用"."来代替)是否是正确的。
先来看一下数独的规则:
There are just 3 rules to Sudoku. |
|
Each row must have the numbers 1-9 occuring just once.
|
|
Each column must have the numbers 1-9 occuring just once.
|
|
And the numbers 1-9 must occur just once in each of the 9 sub-boxes of the grid.
|
很容易得到3个规则:
- 每一行只能出现1~9一次;
- 每一列只能出现1~9一次;
- 每个3×3子区域只能出现1~9一次(子区域之间没有交叉,也就是一共有9个子区域)
解答:
本题直接根据数独的规则“翻译”成代码就可以了:可以设3个长度为9的List,分别代表行、列、子区域。循环每个元素查看是否在相应的List中,如果存在说明重复,不符合规则;如果不存在就把当前元素加入到该List中。如果所有元素循环完毕,说明没有重复值,返回True。该题可以假设输入均合法,即都是1~9或"."。
AC代码:
class Solution(object):
def isValidSudoku(self, board):
row = [[] for _ in xrange(9)]
col = [[] for _ in xrange(9)]
area = [[] for _ in xrange(9)]
for i in xrange(9):
for j in xrange(9):
element = board[i][j]
if element != '.':
# calculate every sub-boxes, map to the left top element
area_left_top_id = i / 3 * 3 + j / 3
if element in row[i] or element in col[j] or element in area[area_left_top_id]:
return False
else:
row[i].append(element)
col[j].append(element)
area[area_left_top_id].append(element)
return True
【LeetCode题意分析&解答】36. Valid Sudoku的更多相关文章
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【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题意分析&解答】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 ...
随机推荐
- 查看MDB格式文件数据表
当打开一个MDB格式的ACCESS文件后,如果里面默认的都是窗体视图,要查看数据表的信息,可以“创建-查询设计”查看表信息,或是在SQL视图中编写SQL语句来实现. 或按着Shift键打开文件.
- VS中,无法嵌入互操作类型“……”,请改用适用的接口的解决方法
最近使用VS,在引用COM组件的时候,出现了无法嵌入互操作类型“……”,请改用适用的接口的错误提示.查阅资料,找到解决方案,记录如下: 选中项目中引入的dll,鼠标右键,选择属性,把“嵌入互操作类型” ...
- 《Oracle Applications DBA 基础》- 9 - Concurrent Processing[Z]
<Oracle Applications DBA 基础>- 9 - Concurrent Processing================================== 参考资料 ...
- oFixed() 方法
oFixed() 方法可把 Number 四舍五入为指定小数位数的数字. 在本例中,我们将把数字舍入为仅有一位小数的数字: Show the number 13.37 with one decimal ...
- 动态sql构建的过程
基本原理:使用xsqlbuilder框架完成动态sql的构建. 基本流程:使用WebUtils.getParametersStartingWith(ServletActionContext.getRe ...
- 关于scala环境配置详解
首先从官网下载适合自身电脑配置的scala安装包.scala下载官网网址:http://www.scala-lang.org/download/ 同时scala还有自己集成好的IDE,例如eclips ...
- VI 配置文件(略全)
配置 ~/.vimrc文件. root则放到/etc/vimrc 具体详见代码 "====================================================== ...
- SQL Server 恢复过程
在恢复过程中.只会分析那些自最后一个检查点之后发生的更改,以确定是否需要重做还是撤销. 在最后一个检查点之前完成的操作都会精确的反应到数据文件中,恢复过程不需要做其它的事. 第一阶段: 分析. 这个阶 ...
- 电池和Adapter切换电路改进实验
目的:很多单电池的机器在大负载的情况下,如把背光开到最亮,运行3D游戏,此时拔DC电源很容易出现机器死机,或花屏现象: 原因:Q5的导通时间不够,希望通过G极的快速放电,加快到导通时间: 修改前的电路 ...
- javascript数组排序-----1
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...