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 ...
随机推荐
- gradle使用心得
gradle是语言式构建,和maven配置型还是差别挺大,琢磨了2天 1.在解析setting.gradle之后,开始解析build.gradle之前,这里如果要干些事情(更改build.gradle ...
- spark-submit 提交Application
在spark1.0中推出spark-submit来统一提交applicaiton ./bin/spark-submit \ --class <main-class> --master &l ...
- python之路07文件处理
一 操作文件的方法: f.read() #读取所有内容,光标移动到文件末尾 f.readline() #读取一行内容,光标移动到第二行首部 f.readlines() #读取每一行内容,存放于列 ...
- centos7的web环境安装配置
1.安装基本东西安装apache yum install httpd安装mariadb yum install mariadb mariadb-server安装php yum install p ...
- 如何在Windows中使用netsh命令进行端口转发
自Windows XP开始,Windows中就内置网络端口转发的功能.任何传入到本地端口的TCP连接(IPv4或IPv6)都可以被重定向到另一个本地端口,或远程计算机上的端口,并且系统不需要有一个专门 ...
- C# 日志记录工具类--LogHelper.cs测试
C# 日志记录工具类:(适用于不想使用log4j等第三方的Log工具的时候,希望自己写个简单类实现)LogHelper.cs内容如下: using System; using System.Diagn ...
- c++官方文档-class
#include <iostream> using namespace std; class Circle { double radius; public: Circle(double r ...
- windows配置ftp服务器
一.搭建FTP 二.解决FTP因windows防火墙拦截的方法 三.配置FTP用户 ========================================================== ...
- css上下或者上中下 自适应布局
方法就是头部不变,中间和底部绝对定位 *{ margin: ; padding: ; } div{ text-align: center; font-size: 30px; } .header,.fo ...
- TTreeView.OnCustomDrawItem
TTreeNode *node; node = , "AAAA"); TreeView1->Items->AddChild(node, "aaa1" ...