Leetcode_36_Valid Sudoku
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42528601
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.
思路:
(1)题意为判断一个数独是否有效。
(2)题意不是让我们求解出整个数独(当然,如果真的求解还是很复杂的),而是判断数独中已有数据是否有效。
(3)本文的思路还是暴力破解,因为没有想到其它好的方法。对数独对应的9*9的二维数组进行遍历,对于任意一个不为'.'的数字,都需要对其所在的行、列、以及3*3的块区域进行判断,如果有重复出现的情况,那么数独就是无效的,具体见下方代码。
(4)希望本文对你有所帮助。
算法代码实现如下:
public boolean isValidSudoku(char[][] board) {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
char curr = board[i][j];
if(curr=='.'){
continue;
}
//行
for (int k = j+1; k < board.length; k++) {
if(curr==board[i][k]){
return false;
}
}
//列
for (int k = i+1; k < board.length; k++) {
if(curr==board[k][j]){
return false;
}
}
//3*3 方块
if(i>=0&&i<3&&j>=0&&j<3){
int count=0;
for (int k1 = 0; k1 < 3; k1++) {
for (int k2 = 0; k2 < 3; k2++) {
if(board[k1][k2]==curr){
count++;
}
if(count>1) return false;
}
}
}
if(i>=0&&i<3&&j>=3&&j<6){
int count=0;
for (int k1 = 0; k1 < 3; k1++) {
for (int k2 = 3; k2 < 6; k2++) {
if(board[k1][k2]==curr){
count++;
}
if(count>1) return false;
}
}
}
if(i>=0&&i<3&&j>=6&&j<9){
int count=0;
for (int k1 = 0; k1 < 3; k1++) {
for (int k2 = 6; k2 < 9; k2++) {
if(board[k1][k2]==curr){
count++;
}
if(count>1) return false;
}
}
}
if(i>=3&&i<6&&j>=0&&j<3){
int count=0;
for (int k1 = 3; k1 < 6; k1++) {
for (int k2 = 0; k2 < 3; k2++) {
if(board[k1][k2]==curr){
count++;
}
if(count>1) return false;
}
}
}
if(i>=3&&i<6&&j>=3&&j<6){
int count=0;
for (int k1 = 3; k1 < 6; k1++) {
for (int k2 = 3; k2 < 6; k2++) {
if(board[k1][k2]==curr){
count++;
}
if(count>1) return false;
}
}
}
if(i>=3&&i<6&&j>=6&&j<9){
int count=0;
for (int k1 = 3; k1 < 6; k1++) {
for (int k2 = 6; k2 < 9; k2++) {
if(board[k1][k2]==curr){
count++;
}
if(count>1) return false;
}
}
}
if(i>=6&&i<9&&j>=0&&j<3){
int count=0;
for (int k1 = 6; k1 < 9; k1++) {
for (int k2 = 0; k2 < 3; k2++) {
if(board[k1][k2]==curr){
count++;
}
if(count>1) return false;
}
}
}
if(i>=6&&i<9&&j>=3&&j<6){
int count=0;
for (int k1 = 6; k1 < 9; k1++) {
for (int k2 = 3; k2 < 6; k2++) {
if(board[k1][k2]==curr){
count++;
}
if(count>1) return false;
}
}
}
if(i>=6&&i<9&&j>=6&&j<9){
int count=0;
for (int k1 = 6; k1 < 9; k1++) {
for (int k2 = 6; k2 < 9; k2++) {
if(board[k1][k2]==curr){
count++;
}
if(count>1) return false;
}
}
}
}
}
return true;
}
Leetcode_36_Valid Sudoku的更多相关文章
- Leetcode 笔记 36 - Sudoku Solver
题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...
- [LeetCode] Sudoku Solver 求解数独
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- [LeetCode] Valid Sudoku 验证数独
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
- LeetCode 36 Valid Sudoku
Problem: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board ...
- 【leetcode】Valid Sudoku
题目简述: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board cou ...
- ACM : POJ 2676 SudoKu DFS - 数独
SudoKu Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu POJ 2676 Descr ...
- ACM: ICPC/CCPC Sudoku DFS - 数独
Sudoku Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/65535K (Java/Other) Total Submis ...
- Leetcode: Sudoku Solver
July 19, 2015 Problem statement: Write a program to solve a Sudoku puzzle by filling the empty cells ...
- Leetcode Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
随机推荐
- PHP 字符串变量
PHP 字符串变量 字符串变量用于存储并处理文本. PHP 中的字符串变量 字符串变量用于包含有字符的值. 在创建字符串之后,我们就可以对它进行操作了.您可以直接在函数中使用字符串,或者把它存储在变量 ...
- Docker常见仓库Ubuntu
Ubuntu 基本信息 Ubuntu 是流行的 Linux 发行版,其自带软件版本往往较新一些. 该仓库提供了 Ubuntu从12.04 ~ 14.10 各个版本的镜像. 使用方法 默认会启动一个最小 ...
- springMVC源码分析--HandlerMethodReturnValueHandler返回值解析器(一)
HandlerMethodReturnValueHandler是用于对Controller中函数执行的返回值进行处理操作的,springMVC提供了多个HandlerMethodReturnValue ...
- [Gradle系列]Gradle打包apk多版本,多渠道,多环境,多功能,多模块随心所欲
Tamic: http://blog.csdn.net/sk719887916/article/details/53411771 开始 上篇Gradle发布Module(Maven)到jcenter, ...
- Zookeeper的安装部署
1.Zookeeper的安装部署 7.1 Zookeeper工作机制 7.1.1.Zookeeper集群角色 Zookeeper集群的角色: Leader 和 follower (Observer ...
- 安卓高级8 SurfaceView案例二 自定义相机
效果:(由于不好录屏所以文字描述) 定一个SurfaceView 下方有几个按钮,点击确定可以拍照保存取消. 并且SurfaceView实时显示相机内容 package qianfeng.com.cu ...
- 豌豆夹Redis解决方案Codis源码剖析:Dashboard
豌豆夹Redis解决方案Codis源码剖析:Dashboard 1.不只是Dashboard 虽然名字叫Dashboard,但它在Codis中的作用却不可小觑.它不仅仅是Dashboard管理页面,更 ...
- Dynamics CRM2016 Web Api之时间字段值的处理
本篇又是一次来谈到CRM中时间字段的问题,那这次要谈的是在引用web api过程中写代码上的注意事项,常用的代码场景即JS和c#. 先来看下js,从下图中可以看到,我直接将new Date()赋值给时 ...
- android M Launcher之LauncherModel (一)
众所周知 LauncherModel在Launcher中所占的位置,它相当于Launcher的数据中心,Launcher的桌面以及应用程序菜单中所需的数据像 桌面小部件的信息.快捷方式信息.文件信息. ...
- Spark技术内幕:Shuffle Read的整体流程
回忆一下,每个Stage的上边界,要么需要从外部存储读取数据,要么需要读取上一个Stage的输出:而下边界,要么是需要写入本地文件系统(需要Shuffle),以供childStage读取,要么是最后一 ...