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 ...
随机推荐
- 二维数组按某个键值排序 FOR PHP
$arr=[ array( 'name'=>'小坏龙', 'age'=>28 ), array( 'name'=>'小坏龙2', 'age'=>14 ), array( 'na ...
- 如果返回结构体类型变量(named return value optimisation,NRVO) ------ 续
为什么? <More C++ idioms>: 3. Algebraic Hierarchy 程序执行的流程与自己想的不一样: Number Number::makeReal(double ...
- flexible.js移动端适配安卓高分辨不兼容问题
根据网上找到的解决办法,对于安卓设备,把dpr=1改为当前设备的dpr if (!dpr && !scale) { if (isIPhone) { // iOS下,对于2和3的屏,用2 ...
- Python的Beautiful Soup简单使用
Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据 Beautiful Soup提供一些简单的.python式的函数用来处理导航.搜索.修改分析树等功能 它是一个工具箱, ...
- Python打包-py2exe
上篇文章讲了pyinstaller,可以打包成包含Windows, Linux, Mac OS X, FreeBSD, Solaris and AIX等操作系统下的可执行文件,如果只针对Windows ...
- 简单了解一下什么是Django或者说Django是做什么的?
Django是什么? Django是一个基于Python的Web应用框架.它与Python的另外一个Web 框架 Flask最大的区别是,它奉行 “包含一切” 的哲学.该理念即为:创建 Web 应用所 ...
- 九个PHP很有用的功能
1. 函数的任意数目的参数 你可能知道PHP允许你定义一个默认参数的函数.但你可能并不知道PHP还允许你定义一个完全任意的参数的函数 下面是一个示例向你展示了默认参数的函数: // 两个默认参数的函数 ...
- [AX]AX2012 Interaction class
Ax2012 Client的form如果属性FormTemplate设置为DetailsPage或者ListPage,则必须同时设置属性InteractionClass为相应的Interaction类 ...
- flexbox子盒子flex属性
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- C#反射基础理解1(转)
反射提供了封装程序集.模块和类型的对象(Type类型) 可以使用反射动态的创建类型的实例,将类型绑定到现有对象,或从现有对象中获取类型,然后,可以调用类型的方法或访问其字段和属性 . 总之,有了反射, ...