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. 【转】Java enum的用法详解

    用法一:常量 在JDK1.5 之前,我们定义常量都是: public static fianl.... .现在好了,有了枚举,可以把相关的常量分组到一个枚举类型里,而且枚举提供了比常量更多的方法. p ...

  2. redis教程(整理中)

    一.redis简介 1.Redis:键值对类型的内存数据库:应用于高并发和实时请求的场景: 2.Redis常用数据类型: (1) string(基本数据类型)     (2)hash 注:hash中的 ...

  3. [Linux] 账户管理命令(一)

    用户和用户组 Linux用户组的所有信息都存放在/etc/group文件中.具有某种共同特征的用户集合起来就是用户组(Group).用户组(Group)配置文件主要有 /etc/group和/etc/ ...

  4. jQuery如何给body绑定事件?

    jQuery如何给body绑定事件? 代码如下: $(document).bind("resize", function () { alert("php-note.com ...

  5. 《CMake实践》笔记三:构建静态库(.a) 与 动态库(.so) 及 如何使用外部共享库和头文件

    <CMake实践>笔记一:PROJECT/MESSAGE/ADD_EXECUTABLE <CMake实践>笔记二:INSTALL/CMAKE_INSTALL_PREFIX &l ...

  6. 【Go入门教程5】面向对象(method、指针作为receiver、method继承、method重写)

    前面两章我们介绍了函数和struct,那你是否想过函数当作struct的字段一样来处理呢?今天我们就讲解一下函数的另一种形态,带有接收者(receiver)的函数,我们称为method method ...

  7. MySQL 5.5编译安装

    MYSQL数据库安装方法 yum/rpm方式安装mysql 只要执行yum install mysql-server即可. yum/rpm方式安装mysql应用场景:yum/rpm安装适用对数据库要求 ...

  8. HDU 3333 | Codeforces 703D 树状数组、离散化

    HDU 3333:http://acm.hdu.edu.cn/showproblem.php?pid=3333 这两个题是类似的,都是离线处理查询,对每次查询的区间的右端点进行排序.这里我们需要离散化 ...

  9. word20161208

    EAP, Extensible Authentication Protocol / 可扩展身份验证协议 EFS, encrypting file system / 加密文件系统 embedded ob ...

  10. 各大搜索引擎智能提示API(JSONP跨域实现自动补全搜索建议)

    ---------------------------------------搜索引擎JSONP接口--------------------------------------------- 提示:U ...