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 ...
随机推荐
- 打印cell的视图层次结构
#ifdef DEBUG NSLog(@"Cell recursive description:\n\n%@\n\n", [cell performSelector:@select ...
- spring - ioc和aop
1.程序中为什么会用到spring的ioc和aop 2.什么是IOC,AOP,以及使用它们的好处,即详细回答了第一个问题 3.原理 关于1: a:我们平常使用对象的时候,一般都是直接使用关键字类new ...
- POJ3749 破译密码
Description 据说最早的密码来自于罗马的凯撒大帝.消息加密的办法是:对消息原文中的每个字母,分别用该字母之后的第5个字母替换(例如:消息原文中的每个字母A都分别替换成字母F).而你要获得消息 ...
- 关于mysql乱码的问题
ALTER TABLE TABLE_NAME CONVERT TO CHARACTER SET utf8 COLLATE UTF8_GENERAL_CI; 第一步,用mysql的自带修复工具在bin文 ...
- 初学structs2,表单验证
一.简单表单验证示例 structs.xml配置 <struts> <package name="validate" namespace="/valid ...
- iconv命令详解
功能] 对于给定文件把它的内容从一种编码转换成另一种编码. [描述] -f encoding :把字符从encoding编码开始转换. -t encoding :把字符转换到encoding编码. ...
- MySql数据类型详解
可配合http://www.cnblogs.com/langtianya/archive/2013/03/10/2952442.html学习 MySql数据类型 1.整型(xxxint) MySQ ...
- jQuery1.11源码分析(7)-----jQuery一些基本的API
这篇文章比较繁杂,主要就是把jQuery源码从上到下列出来,看我的注释就好了. jQuery源码对各种加载器做了处理. //阅读这个源码是请先了解一下概念,即时函数,工厂模式 (function( g ...
- mySQL笔记1
create table 表名( 列名 数据类型 是否为空(是否唯一|是否主键|是否外键) 列名 数据类型 …(最后一列不加逗号) )create database 数据库名 insert into ...
- zstu.4014.水手分椰子(数学推导)
深入浅出学算法015-水手分椰子 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 1827 Solved: 524 Description n个水手来到 ...