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. 解决vs2015使用fopen、fprintf等函数报错的问题

    出现错误提示: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable ...

  2. PR 不能手动修改素材尺寸的解决方法

    选中素材,然后再特效控制台那边点击一下运动就可以在预览窗口直接用鼠标调整画面大小和位移了.

  3. [NOIP2010] 提高组 洛谷P1540 机器翻译

    题目背景 小晨的电脑上安装了一个机器翻译软件,他经常用这个软件来翻译英语文章. 题目描述 这个翻译软件的原理很简单,它只是从头到尾,依次将每个英文单词用对应的中文含义来替换.对于每个英文单词,软件会先 ...

  4. MyEclipse使用SVN进行项目版本控制

    一.搭建SVN服务器. 例如,使用VisualSVN Server,下载后安装. (1)在Repositories(版本库)上右击,新建Repository,选择Regular FSFS reposi ...

  5. CentOS 6.4下安装Oracle 11gR2

    安装前须知: 内存(RAM)的最小要求是 1GB,建议 2GB 及以上. 虚拟内存 swap 建议:内存为 1GB~2GB 时建议swap大小为内存大小的 1.5 倍:内存为 2GB~16GB 时建议 ...

  6. inux环境PHP7.0安装

    inux环境PHP7.0安装   PHP7和HHVM比较PHP7的在真实场景的性能确实已经和HHVM相当, 在一些场景甚至超过了HHVM.HHVM的运维复杂, 是多线程模型, 这就代表着如果一个线程导 ...

  7. 锋利的jQuery-4--停止动画和判断是否处于动画状态(防止动画加入队列过多的办法)

    1.停止元素的动画:stop([cleanQueue, gotoEnd]):第一个参数代表是否要清空未执行完的动画队列,第二个参数代表是否直接将正在执行的动画跳转到末状态. 无参数stop():立即停 ...

  8. Netbeans连接数据库

    /* Netbeans连接数据库 NetBeans项目的“项目属性”中“库”一栏中.Tab页“编译和运行”中已经加上jdbc的驱动文件 */ Connection conn = null;//连接数据 ...

  9. iOS-使用Xcode拉伸图片

    如果要制作一个类似于QQ消息气泡的图片,该如何制作呢?android中可以使用.9图片指定图片中的某一部分拉伸,那iOS中类似的功能要如何实现呢,Xcode提供了类似的功能.具体步骤如下: 1.选择需 ...

  10. github和bitbucket

    注册一个github跟注册一个163的邮箱一样容易 页面中 div方块的 布局和 尺寸, 主要是考虑功能/ 结构/布局, 基本上与其中的内容 的多少无关: 即使内容/文字很少, 也还是要那么宽的尺寸 ...