Java for LeetCode 036 Valid Sudoku
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的更多相关文章
- [LeetCode] 036. Valid Sudoku (Easy) (C++)
指数:[LeetCode] Leetcode 解决问题的指数 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 036. ...
- LeetCode 036 Valid Sudoku
题目要求:Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudo ...
- LeetCode:36. Valid Sudoku,数独是否有效
LeetCode:36. Valid Sudoku,数独是否有效 : 题目: LeetCode:36. Valid Sudoku 描述: Determine if a Sudoku is valid, ...
- Java [leetcode 36]Valid Sudoku
题目描述: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board cou ...
- 【LeetCode】036. Valid Sudoku
题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- 蜗牛慢慢爬 LeetCode 36.Valid Sudoku [Difficulty: Medium]
题目 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- LeetCode 36 Valid Sudoku
Problem: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board ...
- 【leetcode】Valid Sudoku
题目简述: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board cou ...
- LeetCode(38)-Valid Sudoku
题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
随机推荐
- 【BZOJ-1965】SHUFFLE 洗牌 快速幂 + 拓展欧几里德
1965: [Ahoi2005]SHUFFLE 洗牌 Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 541 Solved: 326[Submit][St ...
- asp.net input怎么获取值
前台: <input type="hidden" name="content" value="content"> 后台: Req ...
- POJ2492 A Bug's Life
Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 33833 Accepted: 11078 Description Ba ...
- IIS FTP Server Anonymous Writeable Reinforcement, WEBDAV Anonymous Writeable Reinforcement(undone)
目录 . 引言 . IIS 6.0 FTP匿名登录.匿名可写加固 . IIS 7.0 FTP匿名登录.匿名可写加固 . IIS >= 7.5 FTP匿名登录.匿名可写加固 . IIS 6.0 A ...
- POJ3233Matrix Power Series(十大矩阵问题之三 + 二分+矩阵快速幂)
http://poj.org/problem?id=3233 Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total ...
- jquery------添加jQuery对象方法
my.js $(document).ready(function(){ (function($){ $.fn.swapClass=function(class1,class2){ if(this.ha ...
- Java多线程基础(一)
一.基本概念 线程状态图包括五种状态 1.新建状态(New):线程对象被创建后,就进入新建状态.例如,Thread thread=new Thread(); 2.就绪状态(Runnable):也被称为 ...
- mac 下终端访问文件出现“Permission Denied”解决方案
mac 下终端访问文件出现“Permission Denied”解决方案: 一个文件有3种权限,读.写.可执行,你这个文件没有可执行权限,需要加上可执行权限. 1. 终端下先 cd到该文件的目录下 2 ...
- PHP iconv函数字符串转码导致截断问题
1.iconv函数原型 string iconv ( string $in_charset , string $out_charset , string $str ) in_charset:输入的字符 ...
- Android打电话&发短信
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView ...