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. js获取url信息

    设置或获取对象指定的文件名或路径. alert(window.location.pathname) 设置或获取整个 URL 为字符串. alert(window.location.href); 设置或 ...

  2. webstorm常用快捷键

    常用快捷键—Webstorm入门 提高代码编写效率,离不开快捷键的使用,Webstorm拥有丰富的代码快速编辑功能,你可以自由配置功能快捷键. 快捷键配置 点击“File”-> “setting ...

  3. GIFT-EMS礼记----青软S2SH(笔记)

    这个S2SH的项目,是这本书的一个贯穿项目,所以这里要记录一下, 看这个项目有两个目的: 1.借助这个项目,学习一下S2SH的综合配置及使用 2.借助这个项目练习一下如何做需求分析和项目架构设计. P ...

  4. [NHibernate]ISessionFactory配置

    系列文章 [Nhibernate]体系结构 引言 在上篇文章学习了orm和nhibernate相关概念,这篇文章主要学习ISessionFactory如何配置. 因为NHibernate被设计为可以在 ...

  5. Ubuntu操作系统下软件的卸载

    1.查找安装文件列表 $ dpkg --list 2. 将列表名录复制粘贴到文本文件中 3. 搜索关键词,找到准确的名称 4. 在终端中执行命令: $ sudo apt-get --purge rem ...

  6. iframe的自适应高度

    <iframe src="index.html" id="iframepage" name="iframepage" frameBor ...

  7. nginx:413 Request Entity Too Large 及 修改 PHP上传文件大小配置

    开发环境:CentOS + Nginx + PHP + MySql + phpMyAdmin 在用 phpMyAdmin 进行 sql 数据库导入的时候,经常需要上传比较大的 sql 数据文件,而这时 ...

  8. C和指针 第十三章 习题

    1,1标准输入读入字符,统计各类字符所占百分比 #include <stdio.h> #include <ctype.h> //不可打印字符 int isunprint(int ...

  9. r-cnn学习(四):train_faster_rcnn_alt_opt.py源码学习

    论文看的云里雾里,希望通过阅读其代码来进一步了解. 参考:http://blog.csdn.net/sloanqin/article/details/51525692 首先是./tools/train ...

  10. 解析XML文档

    <html> <body><h1>W3School.com.cn Internal Note</h1><p><b>To:< ...