【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 ...
随机推荐
- 【转】SQL Server查询字段说明
select c .name, isnull(ETP .value, '') as Des FROM syscolumns c inner join systypes t on c. xusertyp ...
- 02151216--Ajax
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 整个网站灰度显示css代码
body *{ -webkit-filter: grayscale(100%); /* webkit */ -moz-filter: grayscale(100%); /*firefox*/ -ms- ...
- IE 第三方设置cookie失效
公司的产品,采用多服务分摊压力,中间必须涉及的当然是单点登陆.一般的单点登陆都是通过去用户中心登陆,302或页面回调的方式,返回到登陆前的页面. 公司项目,想用户体验更好些,采用弹框登陆,可以考虑if ...
- [原创]obj-c编程17:键值观察(KVO)
原文链接:[原创]obj-c编程17:键值观察(KVO) 系列专栏链接:objective-c 编程系列 说完了前面一篇KVC,不能不说说它的应用KVO(Key-Value Observing)喽.K ...
- Oracle EBS-SQL (BOM-6):检查物料失效但BOM中未失效的数据.sql
select msi.segment1 装配件编码 , msi.description 装配件描述 , msi.item_type ...
- Oracle EBS-SQL (BOM-4):检查期间新增编码总数.sql
selectFU.description 创建者,msi.CREATION_DATE ...
- [Leetcode][Python][DP]Regular Expression Matching
# -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/regular-expression-matching/ Implement reg ...
- sql优化-总结
1.尽量缩小数据范围. 2.能一个sql解决的,坚决不用两条sql.利用case when或decode. select month_id, corppkno, sum(exportSum_new) ...
- python 【第四篇】:面向对象(一)
1.前言 提笔忘字,感慨良多!python自习前前后后有一年多了吧,貌似花了不少时间,其实没学到啥东西,都是在面向对象编程之前基础知识这块一直打转转,每次到了面向对象这块就感觉很蒙,看两天直接放弃,从 ...