蜗牛慢慢爬 LeetCode 36.Valid Sudoku [Difficulty: Medium]
题目
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]的更多相关文章
- 蜗牛慢慢爬 LeetCode 20. Valid Parentheses [Difficulty: Easy]
题目 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the i ...
- 蜗牛慢慢爬 LeetCode 6. ZigZag Conversion [Difficulty: Medium]
题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...
- 蜗牛慢慢爬 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 ...
- 蜗牛慢慢爬 LeetCode 22. Generate Parentheses [Difficulty: Medium]
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- 蜗牛慢慢爬 LeetCode 9. Palindrome Number [Difficulty: Easy]
题目 Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...
- 蜗牛慢慢爬 LeetCode 7. Reverse Integer [Difficulty: Easy]
题目 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have ...
- 蜗牛慢慢爬 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 ...
- LeetCode:36. Valid Sudoku,数独是否有效
LeetCode:36. Valid Sudoku,数独是否有效 : 题目: LeetCode:36. Valid Sudoku 描述: Determine if a Sudoku is valid, ...
- LeetCode 36 Valid Sudoku
Problem: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board ...
随机推荐
- OSG漫游到指定坐标点位置
OSG中从当前场景位置漫游到指定点坐标位置,osg中场景的视口状态包括如下参数: 1.视点的位置 2.参考点的位置,该点通常为场景中的中心轴上的点 3.视点向上的方向向量 ( const osg::V ...
- 【转】numpy教程
[转载说明] 本来没有必要转载的,只是网上的版本排版不是太好,看的不舒服.所以转过来,重新排版,便于自己查看. 基础篇 NumPy的主要对象是同种元素的多维数组. 这是一个所有的元素都是一种类型.通过 ...
- 使用开源软件 enfuse 做照片的曝光合成
使用开源软件 enfuse 做照片的曝光合成 所谓曝光合成就是对同一场景用不同的曝光量拍摄多张照片,然后将这些照片再合成为一张照片.之所以我们要这么做是因为现在的相机感光的动态范围相比人眼实在是太小了 ...
- CF 1110 E. Magic Stones
E. Magic Stones 链接 题意: 给定两个数组,每次可以对一个数组选一个位置i($2 \leq i \leq n - 1$),让a[i]=a[i-1]+a[i+1]-a[i],或者b[i] ...
- java异常处理 日志记录异常具体位置的方法
首先要在方法处抛出 Exception异常 然后在方法调用处try catch接收此异常对象 这样就能够记录异常具体位置了 控制台输出: 日志: 要点: System.getProperty(&quo ...
- [BZOJ4484][JSOI2015]最小表示[拓扑排序+bitset]
题意 给你一个 \(n\) 个点 \(m\) 条边的 \(\rm DAG\) ,询问最多能够删除多少条边,使得图的连通性不变 \(n\leq 3\times 10^4\ ,m\leq 10^5\) . ...
- Zabbix实战-简易教程--WEB类--Nginx
一.开启Nginx status状态 1.在默认主机里面加上location添加ngx_status 如下操作: server { listen 127.0.0.1:8080; server_name ...
- java两年工作经验有什么经验
这两年里,了解了完整项目的开发过程,知道如何快速入手一个完全没接触过的项目:譬如先了解数据库关系后,马上熟悉一个功能从前端到后端的实现过程,自己再写一个功 能,这样子就能马上上手开发项目,之后在慢慢了 ...
- JMeter下Groovy和BeanShell语言在不同组件中性能差异实践探究
一般而言JMeter下性能最好的是jar包这类java原生请求,对于JMeter并没有原生支持的请求,一般都会将其直接编译为jar包,然后再JMeter中调用,这样性能最好. 但是有些需求并不适合用j ...
- python爬虫之解析库正则表达式
上次说到了requests库的获取,然而这只是开始,你获取了网页的源代码,但是这并不是我们的目的,我们的目的是解析链接里面的信息,比如各种属性 @href @class span 抑或是p节点里 ...