【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 ...
随机推荐
- mantis 中文统计报表乱码问题解决办法
mantis 中文报表乱码问题 1.安装mantisTB 1.2.17:a.安装插件:管理-->插件管理-->安装MantisGraph(Mantis图表 1.0) 插件b.修改配置文件: ...
- boost json生成和解析用法
json c++库还是有很多的,因为工作上经常使用boost,这里选用boost的json,记录下用法. 举个栗子: 如果我们要生成如下格式的json: { "name":&quo ...
- CEvent,CSemaphore,CCriticalSection,CMutex
一.用CEvent实现线程同步 事件对象(Event)是最简单的同步对象,它包括有信号和无信号两种状态.在线程访问某一资源之前,也许需要等待某一事件的发生,这时用事件对象最合适.例如,只有在通信端口缓 ...
- C++ Primer 读书笔记: 第9章 顺序容器
第9章 顺序容器 引: 顺序容器: vector 支持快速随机访问 list 支持快速插入/删除 deque 双端队列 顺序容器适配器: stack 后进先出栈 queue 先进先出队列 priori ...
- BZOJ 1570: [JSOI2008]Blue Mary的旅行( 二分答案 + 最大流 )
二分答案, 然后对于答案m, 把地点分成m层, 对于边(u, v), 第x层的u -> 第x+1层的v 连边. 然后第x层的u -> 第x+1层的u连边(+oo), S->第一层的1 ...
- 再看static数据成员
当将类的某个数据成员声明为static时,该静态数据成员只能被定义一次,而且要被同类的所有对象共享.各个对象都拥有类中每一个普通数据成员的副本,但静态数据成员只有一个实例存在,与定义了多少类对象无关. ...
- Android 三星手机不能调起应用市场
Uri uri; if (hasAnyMarketInstalled(getContext())) { uri = Uri.parse("market://details?id=" ...
- Oracle EBS-SQL (INV-2):库存会计期间.sql
SELECT STATUS, PERIOD_NAME, PERIOD_NUMBER, PERIOD_YEAR, START_DATE, END_DATE, CLOSE_DATE, REC_TYPE, ...
- Syslog Cisco Incident
http://www.cisco.com/web/about/security/intelligence/identify-incidents-via-syslog.html
- 运行预构建 Linux 映像的 Windows Azure 虚拟机中的交换空间 – 第 1 部分
本文章由 Azure CAT 团队的 Piyush Ranjan (MSFT) 撰写. 随着基础结构服务(虚拟机和虚拟网络)近期在 Windows Azure 上正式发布,越来越多的企业工作负荷正在向 ...