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. MySQL concat用法举例

    concat配合information_schema的应用 1    concat的一般用法主要是用于拼接 示例: 执行语句 SELECT CONCAT('M','y','S','Q','L') 可以 ...

  2. 未来的趋势发展 802.11v网络协议解析

    目前的无线网络中,一个基站通常与拥有最强信号的接入点联系在一起.但是,这个接入点也许过载了.在802.11v标准中,包括了一个指令,接入点能够使用这个指令要求一个基站报告它支持的无线电信道.传输的功率 ...

  3. 20180129周一之学习PYTHON笔记【安装、查看工作目录、】

    一,安装过程中多选一个ADD的项,免去设置环境变量. 二,PYAUTOGUI模块控制键鼠. IMAGE模块. ----------------------python 如何查看与更换工作目录----- ...

  4. JavaScript中判断函数、变量是否存在

    转载:http://www.jb51.net/article/67551.htm 一.是否存在指定函数 function isExitsFunction(funcName) { try { if (t ...

  5. C_point指针

    1,关于C语言中变量的访问方式,直接访问[使用变量名直接引用,操作变量进行赋值,改变变量值等操作],间接访问--指针,一种指向变量飞, 程序对变如量的读写操作,实际是对变量所在的存储空间进行写入和取出 ...

  6. centos上自动执行脚本执行php文件

    1 先编写执行PHP文件的脚本 vi php.sh #!/bin/sh /usr/bin/php /etc/1.php 2把php.sh添加到自动执行任务中 cd /etc/ vi crontab * ...

  7. eclipse模板

    文件(Files)注释标签: /** * @Title: ${file_name} * @Package ${package_name} * @Description: ${todo}(用一句话描述该 ...

  8. spring security 配置xml 参考

    https://blog.csdn.net/zsq520520/article/details/77880491

  9. 3.SpringMVC介绍

    1.采用Spring MVC的好处 Dispathcher Servlet必须做如下的事情: 1.根据URI调用相应的action 2.实例化正确的控制器类 3.根据请求参数值来构造表单bean 3. ...

  10. Error 2503 and 2502 when installing/uninstalling on Windows 10

    1. Hold Ctrl+Shift and press Esc. 2. Locate “Windows Explorer” under “Windows processes”, now right ...