题目

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.

翻译

数独嘛 略

Hints

Related Topics: Hash Table

通过哈希表实现O(n^2)的算法 即只遍历整个数独的表一遍就可以得到有效性判断

代码

C++

class Solution
{
public:
bool isValidSudoku(vector<vector<char> > &board)
{
int used1[9][9] = {0}, used2[9][9] = {0}, used3[9][9] = {0}; for(int i = 0; i < board.size(); ++ i)
for(int j = 0; j < board[i].size(); ++ j)
if(board[i][j] != '.')
{
int num = board[i][j] - '0' - 1, k = i / 3 * 3 + j / 3;
if(used1[i][num] || used2[j][num] || used3[k][num])
return false;
used1[i][num] = used2[j][num] = used3[k][num] = 1;
} return true;
}
};

Java

//an interesting solution from discuss
public boolean isValidSudoku(char[][] board) {
Set seen = new HashSet();
for (int i=0; i<9; ++i) {
for (int j=0; j<9; ++j) {
char number = board[i][j];
if (number != '.')
if (!seen.add(number + " in row " + i) ||
!seen.add(number + " in column " + j) ||
!seen.add(number + " in block " + i/3 + "-" + j/3))
return false;
}
}
return true;
}

Python

class Solution(object):
def isValidSudoku(self, board):
"""
:type board: List[List[str]]
:rtype: bool
"""
row = {}
col = {}
cube = {}
for i in range(0,9):
row[i] = set()
for j in range(0,9):
if j not in col: col[j] = set()
if board[i][j] !='.':
num = int(board[i][j])
k = i/3*3+j/3
if k not in cube: cube[k] = set()
if num in row[i] or num in col[j] or num in cube[k]:
return False
row[i].add(num)
col[j].add(num)
cube[k].add(num)
return True

蜗牛慢慢爬 LeetCode 36.Valid Sudoku [Difficulty: Medium]的更多相关文章

  1. 蜗牛慢慢爬 LeetCode 20. Valid Parentheses [Difficulty: Easy]

    题目 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the i ...

  2. 蜗牛慢慢爬 LeetCode 6. ZigZag Conversion [Difficulty: Medium]

    题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...

  3. 蜗牛慢慢爬 LeetCode 16. 3Sum Closest [Difficulty: Medium]

    题目 Given an array S of n integers, find three integers in S such that the sum is closest to a given ...

  4. 蜗牛慢慢爬 LeetCode 22. Generate Parentheses [Difficulty: Medium]

    题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...

  5. 蜗牛慢慢爬 LeetCode 9. Palindrome Number [Difficulty: Easy]

    题目 Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...

  6. 蜗牛慢慢爬 LeetCode 7. Reverse Integer [Difficulty: Easy]

    题目 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have ...

  7. 蜗牛慢慢爬 LeetCode 1.Two Sum [Difficulty: Easy]

    题目 Given an array of integers, return indices of the two numbers such that they add up to a specific ...

  8. LeetCode:36. Valid Sudoku,数独是否有效

    LeetCode:36. Valid Sudoku,数独是否有效 : 题目: LeetCode:36. Valid Sudoku 描述: Determine if a Sudoku is valid, ...

  9. LeetCode 36 Valid Sudoku

    Problem: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board ...

随机推荐

  1. 为eclipse添加源代码

    看到这个页面,直接点击 红色区域 attach source 关联源代码,进入到如下页面: 点击第二个选择外部的路径,点击导入文件夹,也就是解压出来的src文件夹(不建议直接导整个jar包,虽然也可以 ...

  2. 20155327 java第四周学习笔记

    20155327 java第四周学习笔记 五六章知识整理 1子类与父类 父类是接口或者是抽象类,子类必须继承自父类. 2子类的继承性 在Java中,通过关键字extends继承一个已有的类,被继承的类 ...

  3. RMAN中建立Catalog 用数据库的例子

    RMAN中可以备份metadata到control文件,也可以备份metadata到数据库中,此数据库称为catalog database. 本文参考网上文章,建立一个例子: 使用机器: 机器1:ta ...

  4. PostgreSQL的PITR中,对 unfilled wal log 如何处理为好

    磨砺技术珠矶,践行数据之道,追求卓越价值 回到上一级页面: PostgreSQL基础知识与基本操作索引页     回到顶级页面:PostgreSQL索引页 通过实验,可以发现,PostgreSQL中使 ...

  5. easybcd删除win10启动项如何恢复?

    制作windows启动盘: u盘启动 开始安装,选择左下角的"修复计算机": 运行命令 bcdboot c:\windows /l zh-cn 从系统盘C:\Windows目录中复 ...

  6. 10min 手写一个内存监控系统

    本文的目的在于,尽可能用简单的代码,让大家了解内存监控的原理,及思想.更容易去理解Nagios.Zabbix.Ganglia监控原理,文章最后还有视频教程链接哦,从零敲出来的全过程 思路分为下面几块: ...

  7. Python distribute到底使用package_data还是MANIFEST.in?

    今天看Flask的文档,里面提到如何通过distribute发布你自己的Python包.讲包含文件的时候,提到要用MANIFEST.in并将include_package_data设置为True. 由 ...

  8. 【Python Learning第一篇】Linux命令学习及Vim命令的使用

    学了两天,终于把基本命令学完了,掌握以后可以当半个程序员了♪(^∇^*) 此文是一篇备忘录或者查询笔记,如果哪位大佬看上了并且非常嫌弃的话,还请大佬不吝赐教,多多包涵 以下是我上课做的一些笔记,非常的 ...

  9. ASN.1编码方式详解

    ASN ASN.1 – Abstract Syntax Notation dot one,抽象记法1.数字1被ISO加在ASN的后边,是为了保持ASN的开放性,可以让以后功能更加强大的ASN被命名为A ...

  10. 在WebGL场景中管理多个卡牌对象的实验

    这篇文章讨论如何在基于Babylon.js的WebGL场景中,实现多个简单卡牌类对象的显示.选择.分组.排序,同时建立一套实用的3D场景代码框架.由于作者美工能力有限,所以示例场景视觉效果可能欠佳,本 ...