题目

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. 第五篇(那些JAVA程序BUG中的常见单词)

    The left-hand side of an assignment must be a variable 赋值的左侧必须是变量 left-hand side 左边 assignment 赋值

  2. nmcli 学习小结

    nmcli 是Redhat提供的网络配置编辑工具, 它可直接编辑/etc/sysconfig/network-scripts/ifcfg-xxx , 它是NetworkManager服务的客户端工具, ...

  3. Xors on Segments Codeforces - 620F

    http://codeforces.com/problemset/problem/620/F 此题是莫队,但是不能用一般的莫队做,因为是最优化问题,没有办法在删除元素的时候维护答案. 这题的方法(好像 ...

  4. 嵌套查询--------关联一对多关系----------collection

    参考来源:   http://www.cnblogs.com/LvLoveYuForever/p/6689577.html <resultMap id="BaseResultMap&q ...

  5. 基于Ubuntu14.04下Suricata(一款高性能的网络IDS、IPS和网络安全监控引擎)的搭建(图文详解)(博主推荐)

    为什么,要写这篇论文? 是因为,目前科研的我,正值研三,致力于网络安全.大数据.机器学习研究领域! 论文方向的需要,同时不局限于真实物理环境机器实验室的攻防环境.也不局限于真实物理机器环境实验室的大数 ...

  6. js学习笔记-事件委托

    通过事件委托,你可以把事件处理器绑定到父元素上,避免了把事件处理器添加到多个子级元素上.从而优化性能. 事件代理用到了事件冒泡和目标元素.而任何一个元素的目标元素都是一开始的那个元素. 这里首先要注意 ...

  7. poj2991 Crane

    思路: 线段树每个节点维护第一条线段起点指向最后一条线段终点的向量,于是每一个操作都是一次区间更新.使用成段更新的线段树即可.实现: #include <cstdio> #include ...

  8. Spring------IOC&DI

    一.Spring? Spring兴起:2003年,由Rod Johnson创建.总的来说,Spring Framwork从它诞生至今都一直为人所称道,它的伟大之处自此可见一斑. 核心:IOC& ...

  9. Spark学习之Spark SQL(8)

    Spark学习之Spark SQL(8) 1. Spark用来操作结构化和半结构化数据的接口--Spark SQL. 2. Spark SQL的三大功能 2.1 Spark SQL可以从各种结构化数据 ...

  10. mysql执行语句汇总

    插入select的数据 INSERT INTO `test1`( order_id, goods_id, goods_name, goods_sn, product_id, goods_number, ...