LeetCode:36. Valid Sudoku,数独是否有效
LeetCode:36. Valid Sudoku,数独是否有效 :
**题目: **
LeetCode:36. Valid Sudoku
**描述: **
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.
**分析: **
- 判断一个数独是否有效的判断条件有三个:行、列、子格里都没有重复的1-9数字
思路如下:
1、遍历所有的数组元素,对其中数据进行如下操作:
2、遇到空格也就是“.”时,判断是有效的,并且在定义数组的对应位置上设置其为1,表示已查到该项;
3、首次遇到某数字时,首先设置其在数组中的位置为1,判定有效;
4、当且仅当遇到重复数字时,判定无效。
所以程序应该分为两部分,一部分编写遍历算法,将元素进行三种方式的比对。另一部分用来编写检验是否有效。
5、细节实现
1)遍历算法:
(1)遍历数组元素board[i][j],判断三个条件:
(2)遍历元素对应逻辑九宫格位置:
anRow[j] = board[i][j],
anCloum[i][j] = board[i][j],
anSonSudoKu[j / 3 * 3 + i / 3][j % 3 * 3 + i % 3] = board[i][j]
2) 检验有效(在逻辑九宫格中标识状态位,1位已查询到):
(1)首次遇到某数字时,首先设置其在数组中的位置为1,判定有效
(2)当且仅当遇到重复数字时,判定无效
(3)首次遇到某数字时,首先设置其在数组中的位置为1,判定有效;
**代码: **
bool checkValid(int anArr[], int nVal)
{
// 2) 检验有效:
//(1)首次遇到某数字时,首先设置其在数组中的位置为1,判定有效
//(2)当且仅当遇到重复数字时,判定无效
// (3) 遇到空格也就是“.”时,判断是有效的,并且在定义数组的对应位置上设置其为1,表示已查到该项;
if (nVal < 0) // 判断(3) “.”情况
{
return true;
}
if (1 == anArr[nVal - 1]) // 判断(2) 重复数字情况
{
return false;
}
//(1) 首次遇到某数字
anArr[nVal - 1] = 1;
return true;
}
bool isValidSudoku(const vector<vector<char>>& board)
{
// 1)遍历数组
int anRow[9] = { 0 }; // 用于存储一行的元素比对结果
int anCloum[9] = { 0 }; // 用于存储一列中的元素比对结果
int anSonSudoKu[9] = { 0 }; // 用于存储子格的元素比对
for (int i = 0; i < 9; i++)
{
memset(anRow, 0, sizeof(anRow));
memset(anCloum, 0, sizeof(anCloum));
memset(anSonSudoKu, 0, sizeof(anSonSudoKu));
for (int j = 0; j < 9; j++)
{
if (!checkValid(anRow, board[i][j] - '0') // 检查第i行(0开始计数)
|| checkValid(anCloum, board[j][i] - '0') // 检查第j行(实际为第j列,0开始)
|| checkValid(anSonSudoKu, board[i / 3 * 3 + j / 3][i % 3 * 3 + j % 3] - '0') // 检查 board[i][j] 元素所在 i / 3 * 3 所在行的3个九宫格的三个元素
)
{
return false;
}
}
}
return true;
}
备注:
好记性不如烂笔头!这道题思考了好几天,才发现人和人差距是很大的~
借鉴了tenos大神的解法LeetCode:Valid Sudoku,Sudoku Solver(数独游戏)
LeetCode:36. Valid Sudoku,数独是否有效的更多相关文章
- leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题
三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Sudoku ...
- leetCode 36.Valid Sudoku(有效的数独) 解题思路和方法
Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...
- LeetCode 36 Valid Sudoku
Problem: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board ...
- 蜗牛慢慢爬 LeetCode 36.Valid Sudoku [Difficulty: Medium]
题目 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- Java [leetcode 36]Valid Sudoku
题目描述: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board cou ...
- [LeetCode] 36. Valid Sudoku 验证数独
Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to th ...
- [leetcode]36. Valid Sudoku验证数独
Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to th ...
- LeetCode 36 Valid Sudoku(合法的数独)
题目链接: https://leetcode.com/problems/valid-sudoku/?tab=Description 给出一个二维数组,数组大小为数独的大小,即9*9 其中,未填入 ...
- LeetCode 36. Valid Sudoku (Medium)
题目 Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according ...
随机推荐
- formvalidation表单验证
官方文档:http://formvalidation.io var $formEntityProfileSearch = $("form[name=entityProfileSearch]& ...
- Nagios邮件报警
p.MsoNormal,li.MsoNormal,div.MsoNormal { margin: 0cm; margin-bottom: .0001pt; line-height: 150%; fon ...
- Win10专业版下图片拖到PS无法打开的解决技巧
PS这个软件是用户最常用的软件之一,其强大的图形处理能力毋庸置疑.有用户表示在Win10专业版系统中使用PS发现图片不能直接拖动到PS中打开,这个问题本身不是特别大的问题,但这一小小的毛病会打破用户习 ...
- shiro基础学习(二)—shiro认证
一.shiro简介 shiro是apache旗下一个开源框架,它将软件系统的安全认证相关的功能抽取出来,实现用户身份认证.权限授权.加密.会话管理等功能,组成了一个通用的安全认证框架. 以下 ...
- MyEclipse常见错误
1.Could not create the view: An unexpected exception was thrown java.lang.NullPointerException a ...
- MySQL元数据库——information_schema
平时使用MySQL客户端操作数据库的同学,只要稍微留神都会发现,除了我们建的库之外,还经常看到三个数据库的影子: 1. information_schema 2. performance_schema ...
- 发散问题——Spring容器及加载
一.前言 发散问题系列,是围绕日常工作,发散思考,提取问题,并寻求答案的一个系列.总的来说,就是将遇到的问题发散来提出更多的问题,并通过解决发散问题,从而对问题有更深入的了解,对知识有更深刻的记忆,帮 ...
- Jquery对复选框CheckBox的操作
checkbox: 多选框 //获取选中值 checkbox:$("#checkbox_id").attr("value"): 多选框checkbox,打勾: ...
- IE和其他浏览器用JS新窗口打开的问题
Chrome中 window.open(pageURL,name,parameters) pageURL 为子窗口路径 name 为子窗口句柄 parameters 为窗口参数(各参数用逗号分隔) 例 ...
- 多个git账号的SSH配置
一般使用git都只需要维持一个默认的git账户就可以打天下了. 但如果自己确实需要多个git账号的需求的话,就有必要配置多个ssh key了. 首先为生成多个ssh key ssh-keygen -t ...