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. 34.Android之资源文件res里drawable学习

    我们经常看到android工程资源文件res下drawable如ldpi.mdpi.hdpi.xhdpi.xxhdpi文件,今天我们学习了解下. (1)drawable-hdpi里面存放高分辨率的图片 ...

  2. BZOJ-1067 降雨量 线段树+分类讨论

    这道B题,刚的不行,各种碎点及其容易忽略,受不鸟了直接 1067: [SCOI2007]降雨量 Time Limit: 1 Sec Memory Limit: 162 MB Submit: 2859 ...

  3. POJ1201 Intervals

    Description You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn. Write a p ...

  4. Dancing Links初学记

    记得原来备战OI的时候,WCX大神就研究过Dancing Links算法并写了一篇blog.后来我还写了个搜索策略的小文章( http://www.cnblogs.com/pdev/p/3952279 ...

  5. java将一维数组拆分成二维数组

    package staticFactory; public class Array { public static void main(String[] args) { String[] a=new ...

  6. POJ2392Space Elevator(贪心+背包)

    Space Elevator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9970   Accepted: 4738 De ...

  7. 嵌入式实时操作系统μCOS原理与实践+事件部分代码

    //事件等待表的初始化函数:pevent表示事件控制块的指针#if (OS_EVENT_EN)void  OS_EventWaitListInit (OS_EVENT *pevent){    INT ...

  8. CentOS 6.5(6.4)安装过程图文教程

    CentOS 6.4是最新的出的系统,这里分享下安装教程,有些设置大部分教程没出现过,特分享下,方便需要的朋友 1.首先,要有一张CentOS 6.4的安装介质,使用介质启动电脑出现如下界面 界面说明 ...

  9. php返回json数据简单实例

    <?php include './include/conn.php'; //数据库链接文件 $sql_notice = mysql_query('SELECT * FROM gg_notice ...

  10. WGS84、Web墨卡托、火星坐标、百度坐标互转

    转自:1.http://blog.csdn.net/wildboy2001/article/details/12031351 2.http://kongxz.com/2013/10/wgs-cgj/ ...