题目

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.

分析

这是一道关于数独游戏的题目,首先要了解数独游戏的规则:



所以,对于该题目,有些空格中是’.’ 字符,我们只需要考虑当前状态下是否满足数独即可。

也就是说,我们要按行、按列,按每个3*3宫格,检验三次。

AC代码

class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
if (board.empty())
return false; //数独游戏符合9宫格,也就是为一个9*9的矩阵
int size = board.size(); //根据数独游戏的规则,需要进行三次检验(按行、按列、按照3*3块)
//利用哈希的思想,记录每个关键字的出现次数,若是次数>1,则返回false
vector<int> count;
for (int i = 0; i < size; ++i)
{
//每行开始前,将记录次数的vector清零,元素1~9分别对应下标0~8,对应vector中值为该元素的出现次数
count.assign(9, 0);
for (int j = 0; j < size; j++)
{
if (board[i][j] != '.')
{
int pos = board[i][j] - '1';
if (count[pos] > 0)
return false;
else
++count[pos];
}
else
continue; }//for
}//for //同理,按列检验
for (int j = 0; j < size; j++)
{
count.assign(9, 0);
for (int i = 0; i < size; i++)
{
if (board[i][j] != '.')
{
int pos = board[i][j] - '1'; if (count[pos] > 0)
return false;
else
++count[pos];;
}
else
continue;
}//for
}//for //按3*3小块检验
for (int i = 0; i < size; i += 3)
{
for (int j = 0; j < size; j += 3)
{
count.assign(9, 0);
//每个块又是一个3*3的矩阵
for (int row = i; row < i + 3;row++)
for (int col = j; col < j + 3; col++)
{
if (board[row][col] != '.')
{
int pos = board[row][col] - '1';
if (count[pos] > 0)
return false;
else
++count[pos];;
}
else
continue;
}
}//for
}//for return true;
}
};

GitHub测试程序源码

LeetCode(36)Valid Sudoku的更多相关文章

  1. LeetCode(36): 有效的数独

    Medium! 题目描述: 判断一个 9x9 的数独是否有效.只需要根据以下规则,验证已经填入的数字是否有效即可. 数字 1-9 在每一行只能出现一次. 数字 1-9 在每一列只能出现一次. 数字 1 ...

  2. LeetCode(242)Valid Anagram

    题目 Given two strings s and t, write a function to determine if t is an anagram of s. For example, s ...

  3. LeetCode(49)-Valid Parentheses

    题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...

  4. LeetCode(125) Valid Palindrome

    题目 Given a string, determine if it is a palindrome, considering only alphanumeric characters and ign ...

  5. LeetCode(65) Valid Number

    题目 Validate if a given string is numeric. Some examples: "0" => true " 0.1 " ...

  6. LeetCode(20)Valid Parentheses

    题目 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the i ...

  7. node模拟http服务器session机制-我们到底能走多远系列(36)

    我们到底能走多远系列(36) 扯淡: 年关将至,总是会在一些时间节点上才感觉时光飞逝,在平时浑浑噩噩的岁月里都浪费掉了太多的宝贵.请珍惜! 主题:      我们在编写http请求处理和响应的代码的时 ...

  8. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(36)-文章发布系统③-kindeditor使用

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(36)-文章发布系统③-kindeditor使用 我相信目前国内富文本编辑器中KindEditor 属于前 ...

  9. Windows Phone开发(36):动画之DoubleAnimation

    原文:Windows Phone开发(36):动画之DoubleAnimation 从本节开始,我们将围绕一个有趣的话题展开讨论--动画. 看到动画一词,你一定想到Flash,毕竟WP应用的一个很重要 ...

随机推荐

  1. JS 自写datapage.js 通用分页

    var Page = function () { }; Page.prototype = {     Loading: "<img src='/Content/Scripts/Data ...

  2. c语言程序设计案例教程(第2版)笔记(六)—字符串处理实例

    字符串处理 功能描述:从键盘输入一个文本行后,为用户提供菜单选择,实现字符串一些操作——显示文本行.查找并替换指定子串.删除指定子串.统计指定子串数目. 实现代码: #include<stdio ...

  3. 把sed当作命令解释器使用

    [root@sishen ~]# vim script.sed #!/bin/sed -f #交换第一列和第二列 s/\([^,]*\),\([^,]*\),\(.*\).*/\2,\1, \3/g ...

  4. commons-lang常用工具类StringEscapeUtils使用--转

    https://my.oschina.net/ydsakyclguozi/blog/341496 在apache commons-lang(2.3以上版本)中为我们提供了一个方便做转义的工具类,主要是 ...

  5. JS操作CSS

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. 开源项目:JEECG

    工程下载:https://github.com/zymqqc/jeecg-1

  7. Android 轻松实现语音朗读

    语音朗读,这是一个很好的功能,可以实现一些客户的特殊要求.在Android 实现主意功能只需要几段简单的代码即可完成. 在Android 中使用语音朗读功能 只需要使用此类 TextToSpeech ...

  8. CAS server 连接mysql的deployerConfigContext.xml配置

    1.deployerConfigContext.xml配置 <?xml version="1.0" encoding="UTF-8"?> <b ...

  9. android 2.2 preview3 编译bug

    平台 as2.2 preview3;   1. 解决方法: 在project.buildgradle 里面设置 // Top-level build file where you can add co ...

  10. 半斤八两中级破解 (四) TCP_UDP协议转向本地验证

    首先要用抓包工具判断是哪种协议,根据封包助手来看,教程中给出的例子是个TCP协议的,此时要记录下包的: 源地址,源端口     目的地址,目的端口   源包大小  目的包大小 然后再重新运行抓包工具和 ...