LeetCode 36 Valid Sudoku(合法的数独)
1. 满足每一行的数字都只能是1~9,并且不能产生重复
2. 满足每一列的数字都只能是1~9,并且不能产生重复
3. 满足每一个3*3的正方形块中的数字只能是1~9,并且不能产生重复
package leetcode_50; import java.util.Arrays; /***
*
* @author pengfei_zheng
* 判断数组是否合法
*/
public class Solution36 {
public static boolean isValidSudoku(char[][] board) {
int [][]row = new int[9][9];
int [][]column = new int[9][9];
int [][]cube = new int[9][9];
for(int i = 0 ; i < 9 ; i ++){
Arrays.fill(row[i],0);
Arrays.fill(column[i], 0);
Arrays.fill(cube[i], 0);
}
for(int i = 0 ; i < 9 ; i++){
for(int j = 0; j < 9 ; j++){
if(board[i][j]!='.'){
//tips: char转为对应的数字需要减去48 此处得到对一个数字并且减去1
int num = board[i][j]-49;
int k = i / 3 * 3 + j / 3;
//这里的num表示当前的在该行下是否已经填入过,如果该num已经使用过则返回
if(row[i][num]!=0 || column[j][num]!=0 || cube[k][num]!=0)
return false;
else
row[i][num] = column[j][num] = cube[k][num] = 1;
}
}
}
//只有当全部的判断均正常进行时,才能返回true
return true;
}
}
LeetCode 36 Valid Sudoku(合法的数独)的更多相关文章
- LeetCode:36. Valid Sudoku,数独是否有效
LeetCode:36. Valid Sudoku,数独是否有效 : 题目: LeetCode:36. Valid Sudoku 描述: Determine if a Sudoku is valid, ...
- [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(有效的数独) 解题思路和方法
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 (Medium)
题目 Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according ...
- LeetCode 36. Valid Sudoku (C++)
题目: Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according t ...
随机推荐
- System.web和System.WebServer
System.WebServer是因为iis7而出现的,也就是说如果在Classic下会被忽略,而System.web是iis以前版本的配置. httpModules modules
- workerman定时器使用
From: http://doc3.workerman.net/worker-development/add.html add int \Workerman\Lib\Timer::add(float ...
- Java多线程之细说线程池
前言 在认识线程池之前,我们需要使用线程就去创建一个线程,但是我们会发现有一个问题: 如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了,这样频繁创建线程就会大大降低系统的效率,因 ...
- curl教程2
上篇介绍了curl的基本用法和简单例子,这篇有包含下载,上传,断点续传等的技巧,一并也mark一下吧. 原文:http://blog.csdn.net/apoxlo/article/details/2 ...
- YAML简介
YAML Ain’t Markup Language YAML 是一种简洁的非标记语言 YAML以数据为中心,使用空白,缩进,分行组织数据,从而使得表示更加简洁易读 基本规则: 1.大小写敏感 2. ...
- easyui分页,编辑datagrid某条数据保存以后跳转到某一页
参考资料:http://caizhilin2010.iteye.com/blog/1731698 问题:商品列表页面采用easyui的datagrid展示数据,编辑某行数据保存以后,要求跳转到 用户在 ...
- Windbg在软件调试中的应用
Windbg在软件调试中的应用 Windbg是微软提供的一款免费的,专门针对Windows应用程序的调试工具.借助于Windbg, 我们常见的软件问题:软件异常,死锁,内存泄漏等,就可以进行高效的排查 ...
- myeclipse安装jad反编译插件
有时候想深入底层看jar包封装的源代码,但是打不开.这就需要配置反编译插件: 1:准备原材料 jad.exe + net.sf.jadclipse_3.3.0.jar 下载目录: jad.exe : ...
- war内部结构
war index.html(非必须) WEB-INF classes (java编译之后的class文件) lib(jar文件) web.xml(war包描述文件) subdirectories[可 ...
- python日志,支持彩色打印和文件大小切片写入和写入mongodb
1.项目中使用了自定义的ColorHandler和MongoHandler,使用了内置的RotatingFileHandler和三方库的ConcurrentRotatingFileHandler. 支 ...