LeetCode第[36]题(Java):Valid Sudoku
题目:有效的数独表
难度:Medium
题目内容:
Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:
- Each row must contain the digits
1-9
without repetition. - Each column must contain the digits
1-9
without repetition. - Each of the 9
3x3
sub-boxes of the grid must contain the digits1-9
without repetition.
A partially filled sudoku which is valid.
The Sudoku board could be partially filled, where empty cells are filled with the character '.'
.
Example 1:
Input:
[
["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"]
]
Output: true
Example 2:
Input:
[
["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"]
]
Output: false
Explanation: Same as Example 1, except with the 5 in the top left corner being
modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid.
Note:
- A Sudoku board (partially filled) could be valid but is not necessarily solvable.
- Only the filled cells need to be validated according to the mentioned rules.
- The given board contain only digits
1-9
and the character'.'
. - The given board size is always
9x9
.
翻译:简单翻译下就是,给出一张数独表,判断是否有效。
不需要判断是否有解,只需判断它的三个标准内已经出现的数字是否唯一即可。
我的思路:本题其实不难,不过题目的意思很难懂,仔细理解了就好了。
在三个标准内判断是否唯一,那么就需要三个数组,并且每一个数组都应该是二维的,第一维表示是第几个长条(方形),第二维度表示是长条(方形)内的第几个元素。
三个标准分别是,行、列、块。
对数独表的每一个元素进行循环,每一个元素都在三个标准内都有对应的元素,每次循环都对此三个标准内对应的值设置为true(已经使用),如果发现对应的值已经被设置为true,则立即返回flase表明此表是无效的。
当前值减去“1”则能表示第二维度的值
问题在于如何用元素在数独表的位置【i】【j】表示三个标准第一维度的值:
行:【i】
列:【j】
块: 行数与列数不一样,行数对三的商决定了是从第几个三开始往后数,列数决定了数几个所以为【i/3*3 + j/3】
MyCode:
public boolean isValidSudoku(char[][] board) { boolean[][] row = new boolean[9][9];
boolean[][] column = new boolean[9][9];
boolean[][] block = new boolean[9][9]; for(int i = 0;i<9;i++){
for(int j=0;j<9;j++){
int c = board[i][j] - '1';
if(board[i][j]=='.'){
continue;
}
int loc = i/3*3 + j/3;
if(row[i][c]||column[j][c]||block[loc][c]){
return false;
}
row[i][c] = column[j][c] = block[loc][c] = true;
}
}
return true;
}
我的算法复杂度:O(N2)
编码过程中出现的问题:
1、一开始想用一维数组,然后每个元素都是set,用set来判断,但是发现操作起来比较麻烦。当已经知道要放入的元素的范围,且范围不大的时候应该用数组代替Set。
2、想第二维度如何取值的时候想了很久。当元素为char或者String的数字,用来计数(判唯一)的时候,可以考虑减去“<最小值>”,转为相对值。此处最小值为1。
答案代码:
略,和我的一样。
LeetCode第[36]题(Java):Valid Sudoku的更多相关文章
- LeetCode第[18]题(Java):4Sum 标签:Array
题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...
- LeetCode第[1]题(Java):Two Sum 标签:Array
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2
题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...
- LeetCode第[1]题(Java):Two Sum (俩数和为目标数的下标)——EASY
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- LeetCode之“散列表”:Valid Sudoku
题目链接 题目要求: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku boar ...
- 【Leetcode】【Easy】Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
- leetcode第36题--Sudoku Solver
题目: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated b ...
- LeetCode第[20]题(Java):Valid Parentheses
题目:有效的括号序列 难度:Easy 题目内容: Given a string containing just the characters '(', ')', '{', '}', '[' and ' ...
- LeetCode第[4]题(Java):Median of Two Sorted Arrays 标签:Array
题目难度:hard There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median ...
随机推荐
- CStdioFile.WriteString无法向文件写入中文
CStdioFile.WriteString向文件中写入字符串,但字符串中带有中文的,无法写入. 解决方案: 将带有中文的字符串进行转换后再写入文件. char* pBuffer = NULL; lo ...
- 单源最短距离 Single Source Shortest Path
单源最短距离_示例程序_图模型_用户指南_MaxCompute-阿里云 https://help.aliyun.com/document_detail/27907.html 单源最短距离 更新时间:2 ...
- jd算法大赛 一个user_id只需映射到一个sku_id, 但是一个sku_id能否映射到多个user_id
0-购买预测 w 任务目标 提交user_id-->sku_id (1->1,但反向呢?) 实际操作: 0- 行为表中的type值加权为一个购买意向值0(性别.年龄,不干扰/或加权) 品类 ...
- 常用的SQLalchemy 字段类型
https://blog.csdn.net/weixin_41896508/article/details/80772238 常用的SQLAlchemy字段类型 类型名 python中类型 说明 In ...
- Super Jumping! Jumping! Jumping!---hdu1087(动态规划)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1087 题意就是给你n个数,找出某个序列的最大和,这个序列满足依次增大的规则: 哎,这个题之前做过,但是 ...
- Vue(3)- 安装脚手架、过滤器、生命周期的钩子函数、vue-router基本使用
一.安装脚手架 1.下载node.js,本文下载版本为node-v8.12.0-x64.msi,一键式安装. 2.安装完成后,打开终端,输入node,可进入node环境(两次ctrl+c退出),如下图 ...
- Spring-Hello World实例
Spring Hello World实例 创建Java项目 添加Jar包 创建源文件 现在在Spring项目下创建实际的源文件.首先,要创建一个名为com.tuorialsponit的包,然后在该co ...
- Deep Learning -- 数据增强
数据增强 在图像的深度学习中,为了丰富图像训练集,更好的提取图像特征,泛化模型(防止模型过拟合),一般都会对数据图像进行数据增强,数据增强,常用的方式,就是旋转图像,剪切图像,改变图像色差,扭曲图像特 ...
- 初级dba学习之路参考
今天周一拖着疲惫的身躯 11点才离开公司,回到家估计写完这篇博客就要17号了. 一个人走在回家的路上,很黑,突然很多感触,一个人在北京拼搏,不敢停止学习的脚步,因为只要停下来就会感觉到孤独. 回顾一下 ...
- GIT学习笔记(2):时光机穿梭与远程仓库
GIT学习笔记(2):时光机穿梭与远程仓库 撤销操作 1.GIT如何跟踪修改 在我们修改了代码内容后,执行了git add和git commit命令来将其交由Git进行版本控制.我们前面举的例子是这样 ...