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 ...
随机推荐
- AppScan代理扫描app/H5安全测试(没试过,记录在此)
标签: 1.首先设置AppScan代理,设置如下:
- MariaDB管理系统
MariaDB管理系统 [root@c4kaichen@163 ~]# yum install mariadb[root@c4kaichen@163 ~]# yum install -y mariad ...
- [UE4]使机器人受伤
- kudu和kudu-impala的安装流程
安装apache的kudu 第一步:下载rpm包 http://archive.cloudera.com/kudu/redhat/6/x86_64/kudu/5.11.0/RPMS/x86_64/ 第 ...
- Spark交互式工具spark-shell
REPL Spark REPL Spark shell 下面我们启动一下(我这里搭建的是3节点集群) sc.后面按TAB键可以把提示调出来 查看hdfs上文件内容 这个数据从这里下载的 https:/ ...
- solr 7+tomcat 8 + mysql实现solr 7基本使用(安装、集成中文分词器、定时同步数据库数据以及项目集成)
基本说明 Solr是一个开源项目,基于Lucene的搜索服务器,一般用于高级的搜索功能: solr还支持各种插件(如中文分词器等),便于做多样化功能的集成: 提供页面操作,查看日志和配置信息,功能全面 ...
- python学习之----urllib与urllib2的区分
urllib 还是urllib2 ? 如果你用过Python 2.x 里的urllib2 库,可能会发现urllib2 与urllib 有些不同. 在Python 3.x 里,urllib2 改名为u ...
- Java-IntegerCache
Integer类里面有一个私有的静态内部类IntegerCache类加载时,有一段静态块代码,如下 static final int low = -128; static final int high ...
- 40. Linux下7-zip解压到当前目录的命令
7z x test.zip 解压到当前目录下,但保留原来的目录结构 7z e test.zip 解压到当前目录下,不保留原来的目录结构
- delphi ios grid BindSourceDB bug
BindSourceDB4.DataSet :=nil; BindSourceDB4.DataSet :=FDMemTable1; grid绑定后显示数据正常,第二次赋值BindSourceDB4.D ...