判断一个 9x9 的数独是否有效。只需要根据以下规则,验证已经填入的数字是否有效即可。
数字 1-9 在每一行只能出现一次。
数字 1-9 在每一列只能出现一次。
数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。

上图是一个部分填充的有效的数独。
数独部分空格内已填入了数字,空白格用 '.' 表示。
示例 1:
输入:
[
["5","3",".",".","7",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"]
]
输出: true
示例 2:
输入:
[
["8","3",".",".","7",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"]
]
输出: false
解释: 除了第一行的第一个数字从 5 改为 8 以外,空格内其他数字均与 示例1 相同。
但由于位于左上角的 3x3 宫内有两个 8 存在, 因此这个数独是无效的。

#include <iostream>
#include <vector>
using namespace std; bool isValidSudoku(vector<vector<char>>& board) {
for(int i=;i<;i++)//判断行列是否有重复
{
int a[]={};
int a1[]={};
for(int j=;j<;j++)
{
if(board[i][j]!='.')
if(a[board[i][j]]==-) return false;
else a[board[i][j]]=-;
if(board[j][i]!='.')
if(a1[board[j][i]]==-) return false;
else a1[board[j][i]]=-;
}
} for(int i=;i<;i+=)//判断小的宫格
{
for(int j=;j<;j+=)
{
int a[]={};
for(int k=i;k<i+;k++)
{
for(int h=j;h<j+;h++)
{
if(board[k][h]!='.')
if(a[board[k][h]]==-) return false;
else a[board[k][h]]=-;
}
}
}
}
return true;
} int main() {
// vector<vector<char>> board={{'8', '3', '.', '.', '7', '.', '.', '.', '.'},
// {'6', '.', '.', '1', '9', '5', '.', '.', '.'},
// {'.', '9', '8', '.', '.', '.', '.', '6', '.'},
// {'8', '.', '.', '.', '6', '.', '.', '.', '3'},
// {'4', '.', '.', '8', '.', '3', '.', '.', '1'},
// {'7', '.', '.', '.', '2', '.', '.', '.', '6'},
// {'.', '6', '.', '.', '.', '.', '2', '8', '.'},
// {'.', '.', '.', '4', '1', '9', '.', '.', '5'},
// {'.', '.', '.', '.', '8', '.', '.', '7', '9'}};
// vector<vector<char>> board={
// {'5', '3', '.', '.', '7', '.', '.', '.', '.'},
// {'6', '.', '.', '1', '9', '5', '.', '.', '.'},
// {'.', '9', '8', '.', '.', '.', '.', '6', '.'},
// {'8', '.', '.', '.', '6', '.', '.', '.', '3'},
// {'4', '.', '.', '8', '.', '3', '.', '.', '1'},
// {'7', '.', '.', '.', '2', '.', '.', '.', '6'},
// {'.', '6', '.', '.', '.', '.', '2', '8', '.'},
// {'.', '.', '.', '4', '1', '9', '.', '.', '5'},
// {'.', '.', '.', '.', '8', '.', '.', '7', '9'}
// };
vector<vector<char>> board={{'.', '.', '.', '.', '', '.', '.', '', '.'},
{'.', '', '.', '', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '', '.', '.', ''},
{'', '.', '.', '.', '.', '.', '.', '', '.'},
{'.', '.', '', '.', '', '.', '.', '.', '.'},
{'.', '', '', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '', '.', '.', '.'},
{'.', '', '.', '', '.', '.', '.', '.', '.'},
{'.', '.', '', '.', '.', '.', '.', '.', '.'}};
bool ans=isValidSudoku(board);
std::cout << ans << std::endl;
return ;
}

#leetcode刷题之路36-有效的数独的更多相关文章

  1. python -- leetcode 刷题之路

    第一题 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 15], tar ...

  2. 使用Java+Kotlin双语言的LeetCode刷题之路(三)

    BasedLeetCode LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 ...

  3. 使用Java+Kotlin双语言的LeetCode刷题之路(二)

    BasedLeetCode LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 ...

  4. 使用Java+Kotlin双语言的LeetCode刷题之路(一)

    LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 1 两数之和 给定一个整数数 ...

  5. #leetcode刷题之路40-组合总和 II

    给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合.candidates 中的每个数字在每个组合中只能使用一次.说 ...

  6. #leetcode刷题之路16-最接近的三数之和

    给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数 ...

  7. #leetcode刷题之路13-罗马数字转整数

    罗马数字包含以下七种字符: I, V, X, L,C,D 和 M.字符 数值I 1V 5X 10L 50C 100D 500M 1000例如, 罗马数字 2 写做 II ,即为两个并列的 1.12 写 ...

  8. #leetcode刷题之路6- Z 字形变换

    将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列.比如输入字符串为 "LEETCODEISHIRING" 行数为 3 时,排列如下:L     C     I ...

  9. leetcode 刷题之路 64 Construct Binary Tree from Inorder and Postorder Traversal

    Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...

随机推荐

  1. 自己搭建云盘 – 简单的PHP网盘程序

    自己搭建云盘 – 简单的PHP网盘程序 Veno File Manager 2.6.3 汉化版 相信大家都比较熟悉或使用过 Owncloud 网盘程序,Owncloud 虽强大,不过太过于臃肿,而 V ...

  2. redis 概述、windows版本下载启动访问退出安装、中文乱码、RedisDesktopManager下载

    redis 概述 redis的key是string类型的:value有多种类型,但放入的不是特定类型数据,添加的都是string,只是redis把这些值组织成了各种数据结构.key和相应的值都是str ...

  3. doPost方法不支持 a 标签和地址栏直接输入地址访问

    demo <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...

  4. CSS深入理解之absolute(HTML/CSS)

    absolute和float是同父异母的兄弟,因为它们具有相同点:包裹性与破坏性 absolute的特点 1.独立的,并且可以摆脱overflow的限制,无论是滚动还是隐藏: 2.无依赖,不受rela ...

  5. php 打印

    php 打印功能需要printer.dll文件 扩展下载地址 http://downloads.php.net/pierre/ 这里有很多PHP的扩展

  6. Difference Between Arraylist And Vector : Core Java Interview Collection Question

    Difference between Vector and Arraylist is the most common  Core Java Interview question you will co ...

  7. apache的AllowOverride以及Options使用详解

    通常利用Apache的rewrite模块对 URL 进行重写的时候, rewrite规则会写在 .htaccess 文件里.但要使 apache 能够正常的读取.htaccess 文件的内容,就必须对 ...

  8. Linux FFmpeg(含x264、lame插件)安装记录

    What is FFmpeg? FFmpeg是一套可以用来记录.转换数字音频.视频,并能将其转化为流的开源计算机程序.它提供了录制.转换以及流化音视频的完整解决方案. What is x264? H. ...

  9. windows server 2016 无法联网问题

    首先,联网分解为两个问题,一.WLAN(无线网).二.以太网(有线网) 一 .WLAN问题解决方案 1.打开服务器管理器 2.添加角色和功能 3.一直点下一步到“功能”,勾选 DirectPlay 和 ...

  10. 18年11月5日 NOIP模拟赛

    T1 题解 对于k=100的情况,贪心 对于100%的数据 可以发现,当前的决策只对后面的开采有影响,且剩余耐久度与之后的开采收益成正比,如果倒着考虑这个问题,得出i-n的星球1点耐久度所能获得的最大 ...