【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 ...
随机推荐
- 第一章ASP.NET SignalR简介
第一章ASP.NET SignalR简介 1.1概述: ASP.NET SignalR是微软新开发的类库,为的是帮助ASP.NET开发人员很方便地开发实时网络功能. SignalR允许服务器端和客户端 ...
- Csharp实现快速排序
public void QuickSort(int[] arr, int left, int right) //快速排序 { //先从数列中去处一个数作为基准数 //分区过程,将比这个数大的数全放在他 ...
- sqlite3插入日期时间出错解决
正确写法 insert into hhf_records(RegistrationNumber,MachinesNumber,InDataTime,Flag,CType) values (11,1,d ...
- OC语法10——@protocol协议,
参考资料:博客 @protocol,协议: OC中protocol的含义和Java中接口的含义是一样的,它们的作用都是为了定义一组方法规范. 实现此协议的类里的方法,必须按照此协议里定义的方法规范来. ...
- 利用JSP编程技术实现一个简单的购物车程序
实验二 JSP编程 一.实验目的1. 掌握JSP指令的使用方法:2. 掌握JSP动作的使用方法:3. 掌握JSP内置对象的使用方法:4. 掌握JavaBean的编程技术及使用方法:5. 掌握JSP ...
- hdu 5046 Airport 二分+重复覆盖
题目链接 给n个点, 定义两点之间距离为|x1-x2|+|y1-y2|. 然后要选出k个城市建机场, 每个机场可以覆盖一个半径的距离. 求在选出点数不大于k的情况下, 这个半径距离的最大值. 二分半径 ...
- C# Socket SSL通讯笔记
一.x.509证书 1.制作证书 先进入到vs2005的命令行状态,即:开始-->程序-->Microsoft Visual Studio 2005-->Visual Studio ...
- C++设计模式之装饰者模式
#include "HandCake.h" //手抓饼 HandCake::HandCake() { ; this->name="手抓饼"; } Hand ...
- 【Chromium中文文档】Chrom{e,ium}{,OS}中的硬件视频加速
Chrom{e,ium}{,OS}中的硬件视频加速 转载请注明出处:https://ahangchen.gitbooks.io/chromium_doc_zh/content/zh//General_ ...
- Oracle EBS-SQL (SYS-8):职责定义明细.sql
SELECT DISTINCT fa.application_short_name 模块, b.responsibility_name 职责名称, fa.applica ...


