[leetcode]_Valid Sudoku
中间被启程日本的面试弄的没有静下心来复习算法。这样不好,基本功是硬道理。逐步恢复刷题。
题目:给一个数独(九宫格)中的一些数字,判断该数独是否有效。
即按照数独的规则,判断其行、列、小九格中是否有重复的数字。如有,即判断无效。
直接给代码吧,长期没刷题,代码质量有所下降。
public boolean isValidSudoku(char[][] board) {
int[] help = new int[10]; //横排检测
for(int i = 0 ; i < 9 ; i++){
for(int j = 0 ; j < 9 ; j++){
if(board[i][j] != '.') help[board[i][j] - '0']++;
}
for(int k = 1 ; k <= 9 ; k++){
if(help[k] > 1) return false;
else help[k] = 0;
}
} //竖排检测
for(int row = 0 ; row < 9 ; row++){
for(int col = 0 ; col < 9 ; col++){
if(board[col][row] != '.') help[board[col][row] - '0']++;
}
for(int k = 1 ; k <= 9 ; k++){
if(help[k] > 1) return false;
else help[k] = 0;
}
} //小九宫格检测
int rowStart = 0,rowEnd = 3, colStart = 0 ,colEnd = 3;
while(colStart < 9){
while(rowStart < 9){
for(int row = rowStart ; row < rowEnd ; row++){
for(int col = colStart ; col < colEnd ; col++){
if(board[row][col] != '.') help[board[row][col] - '0']++;
}
}
for(int k = 1 ; k <= 9 ; k++){
if(help[k] > 1) return false;
else help[k] = 0;
}
rowStart += 3;
rowEnd += 3;
}
colStart += 3;
colEnd += 3;
rowStart = 0;
rowEnd = 3;
}
return true;
}
[leetcode]_Valid Sudoku的更多相关文章
- leetcode先刷_Valid Sudoku
我没有看到这个问题,这使其在现货需求数独,害怕一直没敢做.后来我发现原来的标题就是这么简单.推断现在只有数字全不符合的就可以了棋盘上的形势的要求. 是不是正确的三个周期..人是不能满意地看到每一行.每 ...
- [LeetCode] Valid Sudoku 验证数独
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
- LeetCode——Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
- [LeetCode] 37. Sudoku Solver 求解数独
Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy ...
- Leetcode Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
- LeetCode:Valid Sudoku,Sudoku Solver(数独游戏)
Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...
- 【leetcode】Sudoku Solver
Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...
- Java for LeetCode 037 Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- Java [leetcode 37]Sudoku Solver
题目描述: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated ...
随机推荐
- [ActionScript 3.0] AS3.0 获取文本的明暗度
/** * 获取文字的明暗值 * @param t 文字 * @return Number */ function getDensity(t:String):Number { var ttf:Text ...
- ROWNUM-Oracle中的分页代码
SELECT * FROM (SELECT ENAME,SAL,ROWNUM RN FROM EMP WHERE ROWNUM <= @CURRENTPAGE*@PAGESIZE) SUB );
- iOS中使用FMDB事务批量更新数据库
今天比较闲看到大家在群里讨论关于数据库操作的问题,其中谈到了"事务"这个词,坦白讲虽然作为计算机专业的学生,在上学的时候确实知道存储过程.触发器.事务等等这些名词的概念,但是由于毕 ...
- JavaFX Application应用实例
下面代码演示的是JavaFX进程命令行参数的实例.大家可以参阅一下. /*原文地址:http://www.manongjc.com/article/134.html */ import java.ut ...
- Java将Unix时间戳转换成指定格式日期
public String TimeStamp2Date(String timestampString, String formats){ Long timestamp = Long.pars ...
- (Loadrunner)Error: Failed to send data by channels - post message failed.(转)
把Diagnotics-configure-Web Page Diagnotics 设置为 转自: http://www.51testing.com/html/64/371664-3708254.ht ...
- CLOB和BLOB的区别
BLOB和CLOB都是大字段类型,BLOB是按二进制来存储的,而CLOB是可以直接存储文字的.其实两个是可以互换的的,或者可以直接用LOB字段代替这两个.但是为了更好的管理ORACLE数据库,通常像图 ...
- 搭建Artifactory集群
搭建Artifactory集群 制品仓库系统有很多,例如Artifactory.Archiva.Sonatype Nexus.Eclipse Package Drone,其中Artifactory拥有 ...
- 翻译「C++ Rvalue References Explained」C++右值引用详解 Part6:Move语义和编译器优化
本文为第六部分,目录请参阅概述部分:http://www.cnblogs.com/harrywong/p/cpp-rvalue-references-explained-introduction.ht ...
- worker启动executor源码分析-executor.clj
在"supervisor启动worker源码分析-worker.clj"一文中,我们详细讲解了worker是如何初始化的.主要通过调用mk-worker函数实现的.在启动worke ...