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)的更多相关文章

  1. [Leetcode][Python]36: Valid Sudoku

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 36: Valid Sudokuhttps://oj.leetcode.com ...

  2. leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题

    三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Sudoku ...

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

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

  4. 【LeetCode】36. Valid Sudoku 解题报告(Python)

    [LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...

  5. LeetCode 36 Valid Sudoku

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

  6. 36. Valid Sudoku

    ============= Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku b ...

  7. 【LeetCode】36 - Valid Sudoku

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.(http://sudoku.com.au/TheRu ...

  8. Java [leetcode 36]Valid Sudoku

    题目描述: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board cou ...

  9. 【LeetCode题意分析&解答】36. Valid Sudoku

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

随机推荐

  1. Centos7安装docker(转!)

    时间在自己的运动中也会碰到挫折,遇到障碍,所以某一段时间也会滞留在哪一个房间里 <百年孤独> 转自:https://www.cnblogs.com/yufeng218/p/8370670. ...

  2. OPatch failed with error code 73(OracleHomeInventory gets null oracleHomeInfo)

    OPatch failed with error code 73(OracleHomeInventory gets null oracleHomeInfo) 1.问题描述 [oracle@dou_ra ...

  3. react 中state与props

    react 中state与props 1.state与props props是只读属性,只有在组件被实例化的时候可以赋值,之后的任何时候都无法改变该值.如果试图修改该值时,控制台会报错 only re ...

  4. Salesforce开源TransmogrifAI:用于结构化数据的端到端AutoML库

    AutoML 即通过自动化的机器学习实现人工智能模型的快速构建,它可以简化机器学习流程,方便更多人利用人工智能技术.近日,软件行业巨头 Salesforce 开源了其 AutoML 库 Transmo ...

  5. 学习MongoDB 四: MongoDB查询(一)

    一.简介 MongoDB提供了db.collection.find() 方法可以实现根据条件查询和指定使用投影运算符返回的字段省略此参数返回匹配文档中的所有字段. 二.db.collection.fi ...

  6. Redis安装测试

    1.在线下载,redis是C语言开发的,编译需要依赖一个gcc的环境: yum install gcc-c++,需要保证网络畅通,在线安装: 键入y 环境安装完成后,接下来安装redis; 可以先去h ...

  7. python拓展2 collections模块与string模块

    知识内容 1.collections模块介绍 2.collections模块使用 3.string模块介绍及使用 一.collections模块介绍 collections模块中提供了很多python ...

  8. tornado--同步异步

    同步:指两个或两个以上随时间变化的量在变化过程中保持一定的相对关系 现象:有一个共同的时钟,按来的顺序一个一个处理 异步:双方不需要共同的时钟,也就是接收方不知道发送方什么时候发送,所以在发送的信息中 ...

  9. JS修改属性,六种数据类型

    JS修改属性 一般修改单个属性是通过JS修改的,比较方便.改多个属性通过css样式改更方便. 1.特殊:通过JS修改包含"-"符号的属性,例如margin-top // 特殊 修改 ...

  10. UVA-712-满二叉树

    一个策略树(S-tree)是一组变量Xn={x1,x2...xn}的表现形式,它代表一个布尔函数f:{0,1}n->{0,1},策略树每条路径从根结点开始由n+1个结点组成,策略树的每一个结点都 ...