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. 每一行只能出现1~9一次;
  2. 每一列只能出现1~9一次;
  3. 每个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的更多相关文章

  1. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  2. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  3. 【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 ...

  4. 【LeetCode题意分析&解答】38. Count and Say

    The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...

  5. 【LeetCode题意分析&解答】43. Multiply Strings

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  6. 【LeetCode题意分析&解答】42. Trapping Rain Water

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  7. 【LeetCode题意分析&解答】41. First Missing Positive

    Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0]  ...

  8. 【LeetCode题意分析&解答】39. Combination Sum

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...

  9. 【LeetCode题意分析&解答】34. Search for a Range

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

随机推荐

  1. win10 ie11 以管理员身份运行才正常

    和谐版 win10优化后 ie11不能下载 显示不正常, 以管理员身份运行才正常 ,网上攻略 ( “打开并修改注册表使用快捷键[WIN+R]打开命令行窗口,输入regedit打开注册表,在注册表中找H ...

  2. mvc 设置默认页技巧

    打开网址:http://xxxx.com自动跳转==>http://xxx.com/home/index 设置route入口: routes.MapRoute( name: "Main ...

  3. STL源码剖析 迭代器(iterator)概念与编程技法(三)

    1 STL迭代器原理 1.1  迭代器(iterator)是一中检查容器内元素并遍历元素的数据类型,STL设计的精髓在于,把容器(Containers)和算法(Algorithms)分开,而迭代器(i ...

  4. Javascript 学习笔记 无缝滚动

    效果 : 鼠标移入图片 停止滚动, 鼠标移出自动滚动 可以调整向左或右方向滚动 <style type="text/css"> * { margin:; padding ...

  5. C++静态库与动态库(简介)

    C++静态库与动态库 这次分享的宗旨是——让大家学会创建与使用静态库.动态库,知道静态库与动态库的区别,知道使用的时候如何选择.这里不深入介绍静态库.动态库的底层格式,内存布局等,有兴趣的同学,推荐一 ...

  6. 请求(Request)的参数(Parameter)里包含特殊字符(#等)的正确处理方式

    遇到一个问题 在一个地址链接(URL)里使用 url?param1=val1&param2=val2 的方式传递参数,结果在获取参数值时发现不是当初设定的值. 具体案例 以特殊字符井号(#)为 ...

  7. J2SE知识点摘记(十八)

    Java容器类类库的用途是“保存对象”,并将其划分为两个不同的概念: 1)  Collection . 一组对立的元素,通常这些元素都服从某种规则.List必须保持元素特定的顺序,而Set 不能有重复 ...

  8. C语言数据结构----栈的应用(程序的符号匹配检测)

    本节主要讲利用栈来实现一个程序中的成对出现的符号的检测,完成一个类似编译器的符号检测的功能,采用的是链式栈. 一.问题的提出以及解决方法 1.假定有下面一段程序: #include <stdio ...

  9. kafka学习(二)-zookeeper集群搭建

    zookeeper概念 ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,它包含一个简单的原语集,分布式应用程序可以基于它实现同步服务,配置维护和命名 服务等.Zookeeper是h ...

  10. ReactNative遇到的一些坑

    1. 如果通过一个url进行下载上传操作,这个url中包含有中文的话,一定要记得将这个url转为URLEncode编码. 如: encodeURI('http://test/有中文.doc'); 2. ...