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 ...
随机推荐
- datatables,表格
官方文档:https://datatables.net/ var dttblTaskOrderOptions={ order: [5, 'desc'], ajax:{ url:"order/ ...
- 【Spring】BeanFactory解析bean详解
在该文中来讲讲Spring框架中BeanFactory解析bean的过程,该文之前在小编原文中有发表过,要看原文的可以直接点击原文查看,先来看一个在Spring中一个基本的bean定义与使用. pac ...
- 微信小程序首页总结
效果图如下 首先从大的方面来讲,就是设置了window的属性 "navigationBarBackgroundColor": "#AFE2E6",//bar ...
- python 日期 & 时间
1. Python 提供了一个 time 和 calendar 模块可以用于格式化日期和时间. 2. 时间间隔是以秒为单位的浮点小数. 3. 每个时间戳都以自从1970年1月1日午夜(历元)经过了多长 ...
- ls命令的20个实用范例
contents ls -l -h -lhS -l --block-size=M -a -d */ -g -G -n --color=never -i -p -r -R -t ls ~ ls --ve ...
- SpringCloud网关ZUUL集成consul
最近一直在搞基于springcloud的微服务开发,为了不限定微服务开发语言,服务发现决定采用consul不多说上代码 pom文件 <project xmlns="http://mav ...
- ECP系统J2EE架构开发平台
一 体系结构 ECP平台是一个基于J2EE架构设计的大型分布式企业协同管理平台,通过采用成熟的J2EE的多层企业架构体系,充分保证了系统的健壮性.开放性和扩展性.可选择部署于多种系统环境,满足不同类型 ...
- druid 连接kafuk
java -Xmx256m -Duser.timezone=UTC -Dfile.encoding=UTF-8 -Ddruid.realtime.specFile=examples/indexing/ ...
- vue-router2 使用
VUE-ROUTER2 API http://router.vuejs.org/zh-cn/api/router-link.html 1,安装vue-router npm install vue ...
- mysql数据库实操笔记20170418
一.建立商品分类表和价格表: 1.分类表`sankeq``sankeq`CREATE TABLE cs_mysql11(id INT(11) NOT NULL AUTO_INCREMENT,categ ...