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.

玩过九宫格的都应该知道规则(没玩过的可以试玩一下九宫格

(1)每行1~9各出现一次

(2)每列1~9各出现一次

(3)每个小的3宫格,1~9各出现一次

class Solution {
public: bool isValidRow(vector<vector<char> >& board){
for(int row = ; row < ; ++ row){
vector<int> cnt(,);
for(int col = ; col < ; ++ col){
char item = board[row][col];
if(item != '.'){
if(cnt[item-'']!=) return false;
else cnt[item-'']++;
}
}
}
return true;
} bool isValidCol(vector<vector<char> >& board ){
for(int col = ; col < ; ++ col){
vector<int> cnt(,);
for(int row = ; row < ; ++ row){
char item = board[row][col];
if(item != '.'){
if(cnt[item-'']!=) return false;
else cnt[item-'']++;
}
}
}
return true;
} bool isValidBox(vector<vector<char> >& board){
for(int i = ; i < ; ++ i){
for(int j = ; j < ; ++ j){
vector<int> cnt(,);
for(int row = *i;row < *i+; ++row){
for(int col = *j; col < *j+; ++col){
char item = board[row][col];
if(item != '.'){
if(cnt[item-'']!=) return false;
else cnt[item-'']++;
}
}
}
}
}
return true;
} bool isValidSudoku(vector<vector<char> > &board) {
return isValidRow(board)&&isValidCol(board)&&isValidBox(board);
}
};

Leetcode Valid Sudoku的更多相关文章

  1. LeetCode——Valid Sudoku

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

  2. LeetCode:Valid Sudoku,Sudoku Solver(数独游戏)

    Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...

  3. LeetCode: Valid Sudoku 解题报告

    Valid SudokuDetermine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku boa ...

  4. [LeetCode] Valid Sudoku 验证数独

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

  5. [Leetcode] valid sudoku 有效数独

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

  6. Leetcode | Valid Sudoku & Sudoku Solver

    判断valid,没有更好的方法,只能brute force. class Solution { public: bool isValidSudoku(vector<vector<char& ...

  7. leetcode Valid Sudoku python

    #数独(すうどく,Sūdoku)是一种运用纸.笔进行演算的逻辑游戏.玩家需要根据9×9盘面上的已知数字,推理出所有剩余空格的数字,并满足每一行.每一列.每一个粗线宫内的数字均含1-9,不重复.#数独盘 ...

  8. [LeetCode]Valid Sudoku解题记录

    这道题考查对二维数组的处理,哈希表. 1.最自然的方法就是分别看每一个数是否符合三个规则.所以就须要对应的数据结构来 记录这些信息,判定是否存在.显然最先想到用哈希表. 2.学会把问题抽象成一个个的子 ...

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

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

随机推荐

  1. Daily Build

    Daily Build 是一件非常有意义的事情,也是敏捷开发中关于 “持续集成” 的一个实践.Daily Build 对于开发来说有如下好处: 保证了每次 check in 的代码可用,不会造成整个工 ...

  2. 统计SqlServer每张表内的数据量

    CREATE TABLE #temp (TableName VARCHAR (255), RowCnt INT)EXEC sp_MSforeachtable 'INSERT INTO #temp SE ...

  3. can't debug windows service in win7 64bit

    if encount below error: Solution: run the command “vsdiag_regwcf.exe -i” as admin in C:\Program File ...

  4. 利用PHP的register_shutdown_function来记录PHP的输出日志,模拟析构函数

    最近在做的一个项目,由于全是通过远程HTTP请求来调用PHP的接口程序. 接收到的参数和返回的内容对开发人员来说都是未知不可见的. 虽然可以通过直接在脚本中模拟请求,但由于实际环境复杂的多,调试极其不 ...

  5. MySQL 的 find_in_set 函数使用方法

    举个例子来说: 有个文章表里面有个type字段,它存储的是文章类型,有 1头条.2推荐.3热点.4图文...1,12,13 等等 . 现在有篇文章他既是 头条,又是热点,还是图文, type中以 1, ...

  6. windows vim修改字体

    C:\Program Files (x86)\Vim\vim74 目录下,在vimrc_example.vim和mswin.vim中添加: set guifont=Consolas:h11

  7. php之获取程序源码的方法

    文件hello.php和index.php在同一目录 hello.php <?php class hello{ public function __construct() { echo 'hel ...

  8. js 的Location对象

    Location对象 location用于获取或设置窗体的URL,并且可以用于解析URL. 语法: location.[属性|方法] location对象属性图示: location 对象属性: ha ...

  9. 《C#高级编程(第六版)》泛型学习笔记(一):泛型优点和特性 (转载)

    原文出处:http://www.cnblogs.com/xun126/archive/2011/01/13/1933838.html 泛型是CLR 2.0的一个新特性,在CLR 1.0中,要创建一个灵 ...

  10. 不会JavaScript能混前端么?能拿到高薪么?

    不会JavaScript能混前端么?能拿到高薪么? 答案是:很难! 为什么这么说呢,理由很简单 1)有很多企业不注重前端开发,当然也就不会给你高薪了,这样的企业以小企业居多. 他们一般对前端开发的认识 ...