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. Property和attribute的区别[转]

    Attribute和Property都可以翻译成“属性”,有的地方用Attribute表示“属性”,有的地方又在用Property,初 学者常常在这两个单词间“迷失”,甚至认为二者没有区别,是一样的. ...

  2. ThinkPHP2.2框架执行流程图,ThinkPHP控制器的执行流程

    ThinkPHP2.2框架执行原理.流程图在线手册 ThinkPHP控制器的执行流程 对用户的第一次URL访问 http://<serverIp>/My/index.php/Index/s ...

  3. delphi xe4 程序添加管理员权限要求后不能调试的解决方法

    环境: win7 企业版 xe4 问题: 把项目设置为需要管理员权限才能运行后,调试会弹出一个提示框,如图:

  4. java2

    1:关键字(掌握) (1)被Java语言赋予特定含义的单词 (2)特点: 全部小写. (3)注意事项: A:goto和const作为保留字存在. B:类似于Notepad++这样的高级记事本会对关键字 ...

  5. git pull request

    如何发 PR 以下以 wiki-pages 为例 把项目 fork 到自己名下,然后 clone 到本地 git clone git@code.xiaojukeji.com:yexiliang/wik ...

  6. 拨打电话tel: 跳转到邮件mailto:(html)

    拨打电话 <a href="tel://0571866000">0571-866000</a> 跳转到邮件 <a href="mailto: ...

  7. [ubuntu]--vim命令

  8. URL类型入参串调用接口

    最近通过调用另一个合作公司提供的接口实现方法,借鉴同事之前编写的方法 Models.JSON.Patient类中有各种属性,也可增加属性来满足新需求 public string TakeAppoint ...

  9. CoreAnimation 之CATextLayer

    如果你想在一个图层中显示文字,完全可以借助图层代理直接将Core Graphics写入图层的内容(这就是UILabel的精髓).如果雨果寄宿于图层的视图,直接在图层上操作,其实相当繁琐.你要为每一个显 ...

  10. web前端学习部落22群 明白何谓Margin Collapse

    明白何谓Margin Collapse 不同于其他很多属性,盒模型中垂直方向上的Margin会在相遇时发生崩塌,也就是说当某个元素的底部Margin与另一个元素的顶部Margin相邻时,只有二者中的较 ...