36. Valid Sudoku (Array; HashTable)
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.
class Solution {
public:
bool isValidSudoku(vector<vector<char> > &board) {
vector<bool> flag(, false); //indicate the appearance of 1,2,..., 9
//check line
for(int i = ; i<; i++)
{
for(int j = ; j<; j++)
{
if(board[i][j]=='.') continue;
if(flag[board[i][j]-'']) return false;
flag[board[i][j]-''] = true;
}
flag.assign(board.size(),false); //reset the vector
}
//check column
for(int j = ; j<; j++)
{
for(int i = ; i<; i++)
{
if(board[i][j]=='.') continue;
if(flag[board[i][j]-'']) return false;
flag[board[i][j]-''] = true;
}
flag.assign(board.size(),false);
}
//check small square
for(int i = ; i < ; i+=){
for(int j = ; j <; j+=){
for(int m = ; m < ; m++){
for(int n = ; n < ; n++){
if(board[i+m][j+n]=='.') continue;
if(flag[board[i+m][j+n]-'']) return false;
flag[board[i+m][j+n]-''] = true;
}
}
flag.assign(board.size(),false);
}
}
return true;
}
};
如果没有'.',那么我们可以用以下的方法判断是否valid (参数是某一行,某一列,或是某一个small square的9个元素)
bool check(vector<int> v) {
sort(v.begin(), v.end());
for (int i = ; i < (int)v.size(); ++i) {
if (v[i] != i + ) {
return false;
}
}
return true;
}
36. Valid Sudoku (Array; HashTable)的更多相关文章
- [Leetcode][Python]36: Valid Sudoku
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 36: Valid Sudokuhttps://oj.leetcode.com ...
- leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题
三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Sudoku ...
- LeetCode:36. Valid Sudoku,数独是否有效
LeetCode:36. Valid Sudoku,数独是否有效 : 题目: LeetCode:36. Valid Sudoku 描述: Determine if a Sudoku is valid, ...
- 【LeetCode】36. Valid Sudoku 解题报告(Python)
[LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...
- LeetCode 36 Valid Sudoku
Problem: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board ...
- 36. Valid Sudoku
============= Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku b ...
- 【LeetCode】36 - Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.(http://sudoku.com.au/TheRu ...
- Java [leetcode 36]Valid Sudoku
题目描述: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board cou ...
- 【LeetCode题意分析&解答】36. Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
随机推荐
- python 用到的函数记录
1. ctime() 获取当前的时间 2. import random random.randint(0,99) 随机产生0到99之间的数值 (包含0和99) (整数!!) 3. 往列表添加数值 l ...
- 1019 General Palindromic Number (20 分)
1019 General Palindromic Number (20 分) A number that will be the same when it is written forwards or ...
- Unreal Engine 4(虚幻UE4)GameplayAbilities 插件入门教程(六)GameplayEffect的级别设置
本节的内容不难,权当是复习.如果没有完成前面的教程,请前往学习. 第一步:用一个csv文件表示级别数据,下图中的Hurt随级别1~7表示其损伤值在1~7级别时分别是-7,-14,-20等.写好之后关闭 ...
- CA证书认证单向和双向的区别
我觉得最科学的应该是,单向的,每次客户端发两把锁住的东西给服务端,服务端解密两次,服务端用客户端发来的对称密钥加密数据,发送给客户端,客户端只需解密一次,然后客户端每次修改随机密码,传给服务端,服务 ...
- Map 合并
比如说 qq.com 100 163.com 90 QQ.COM 10 Qq.Com 5 …… 如果统计的话,需要忽略大小写的,即 QQ邮箱总共是100+10+5,怎么写? 其实这个应该不难的,就 ...
- vb 导出excel生成图表统计
Private Sub btnExprot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnE ...
- [Dart] Flutter开发中的几个常用函数
几个Flutter开发中的常用函数 /** 返回当前时间戳 */ static int currentTimeMillis() { return new DateTime.now().millisec ...
- 得到当前对象在不同大小的页面中的绝对位置,及冒泡cancelBubble
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- Latex Error:‘acmart.cls’ not found 解决方案:
windows下latex编译ACM论文模板时,出现Latex Error:‘acmart.cls’ not found,解决方案: 首先cd至模板所在目录下,然后运行以下命令: tex acma ...
- zabbix监控系统日志
监控日志必须让agent运行在主动模式 参考网站:https://www.cnblogs.com/dadonggg/p/8611054.html?from=singlemessage