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 '.'.

解题思路:

传说中的数独(九宫格)问题,老实遍历三个规则即可:

JAVA实现:

	static public boolean isValidSudoku(char[][] board) {
for (int i = 0; i < board.length; i++) {
HashMap<Character, Integer> hashmap = new HashMap<Character, Integer>();
for (int j = 0; j < board[0].length; j++) {
if (board[i][j] != '.') {
if (hashmap.containsKey(board[i][j]))
return false;
hashmap.put(board[i][j], 1);
}
}
}
for (int j = 0; j < board[0].length; j++) {
HashMap<Character, Integer> hashmap = new HashMap<Character, Integer>();
for (int i = 0; i < board.length; i++) {
if (board[i][j] != '.') {
if (hashmap.containsKey(board[i][j]))
return false;
hashmap.put(board[i][j], 1);
}
}
}
for (int i = 0; i < board.length; i += 3){
for (int j = 0; j < board[0].length; j += 3){
HashMap<Character, Integer> hashmap = new HashMap<Character, Integer>();
for (int k = 0; k < 9; k++) {
if (board[i + k / 3][j + k % 3] != '.') {
if (hashmap.containsKey(board[i + k / 3][j + k % 3]))
return false;
hashmap.put(board[i + k / 3][j + k % 3], 1);
}
}
}
}
return true;
}

Java for LeetCode 036 Valid Sudoku的更多相关文章

  1. [LeetCode] 036. Valid Sudoku (Easy) (C++)

    指数:[LeetCode] Leetcode 解决问题的指数 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 036. ...

  2. LeetCode 036 Valid Sudoku

    题目要求:Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudo ...

  3. LeetCode:36. Valid Sudoku,数独是否有效

    LeetCode:36. Valid Sudoku,数独是否有效 : 题目: LeetCode:36. Valid Sudoku 描述: Determine if a Sudoku is valid, ...

  4. Java [leetcode 36]Valid Sudoku

    题目描述: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board cou ...

  5. 【LeetCode】036. Valid Sudoku

    题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  6. 蜗牛慢慢爬 LeetCode 36.Valid Sudoku [Difficulty: Medium]

    题目 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  7. LeetCode 36 Valid Sudoku

    Problem: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board ...

  8. 【leetcode】Valid Sudoku

    题目简述: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board cou ...

  9. LeetCode(38)-Valid Sudoku

    题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

随机推荐

  1. 【HDU 1445】Ride to School

    题 题意 骑自行车,等0时开始最早出发的人,一起出发,然后被别人超过时,就追上去,终点距离是4.5km,速度单位是km/s,求到达的时间(s). 分析 贪心,找0时开始最早到的即可. 代码 #incl ...

  2. 解决SSH无密码登陆后又需要密码登陆

    主节点CentOS_Master 从节点Slave_1. 我想着可能是 /etc/ssh/sshd_config下的那个公钥文件路径不对,看了下home/hxsyl/.ssh/authorized_k ...

  3. 【bzoj1179】 Apio2009—Atm

    www.lydsy.com/JudgeOnline/problem.php?id=1179 (题目链接) 题意 给出一张有向图,每个节点有点权.标记一些点,找出一条路径,可以重复经过一条边,使得总点权 ...

  4. codevs1064 虫食算

    题目描述 Description 所谓虫食算,就是原先的算式中有一部分被虫子啃掉了,需要我们根据剩下的数字来判定被啃掉的字母.来看一个简单的例子: 43#9865#045    +    8468#6 ...

  5. poj 3463 最短路与次短路&&统计个数

    题意:求最短路和比最短路长度多1的次短路的个数 本来想图(有)方(模)便(版)用spfa的,结果妹纸要我看看dijkstra怎么解.... 写了三遍orz Ver1.0:堆优化+邻接表,WA //不能 ...

  6. POJ1523 SPF

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8254   Accepted: 3772 Description Consi ...

  7. Model1模式的学生信息增删改查

    Student.java package entity; public class Student { private int stuid; private String stuname; priva ...

  8. javascript设计模式-装饰模式

    装饰模式:在不改变原类(对象)和继承的情况下动态扩展对象功能,通过包装一个对象来实现一个新的具有原对象相同接口的新的对象.在设计原则中,有一条,多用组合,少用继承,装饰模式正是这一原则的体现. UML ...

  9. 项目总结—jQuery EasyUI-DataGrid动态加载表头

    http://blog.csdn.net/zwk626542417/article/details/19248747 概要 在前面两篇文章中,我们已经介绍了在jQuery EasyUI-DataGri ...

  10. classpath、path、JAVA_HOME的作用

    CLASSPATH是什么?它的作用是什么? 它是javac编译器的一个环境变量. 它的作用与import.package关键字有关. 当你写下improt java.util.*时,编译器面对impo ...