[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 ...
随机推荐
- Regex.Replace的基本用法
Regex构造函数Regex(string pattern)Regex(string pattern,RegexOptions options)参数说明pattern:要匹配的正则表达式模式optio ...
- Mingyang.net:No identifier specified for entity
org.hibernate.AnnotationException: No identifier specified for entity: net.mingyang.modules.system.C ...
- poj 2406 Power Strings kmp算法
点击打开链接 Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 27368 Accepted: ...
- Xpath定位大全
selenium使用Xpath定位之完整篇 其中有一片文章提到了xpath元素定位,但是该文章中有些并不能适应一些特殊与个性化的场景.在文本中提供xpath元素的定位终极篇,你一定能在这里找到你需 ...
- startService()和bindService()区别
1. 生命周期:startService()方式启动,Service是通过接受Intent并且会经历onCreate()和onStart().当用户在发出意图使之销毁时会经历onDestroy(),而 ...
- maven + appium + testng + java之pom.xml
参考来源:<https://search.maven.org/remotecontent?filepath=io/appium/java-client/3.3.0/java-client-3.3 ...
- EndNote文献管理
一直想写个博客,但是一直没有好好坐下来对自己工作进行一个梳理.从今天开始吧,争取多写一点. 今天,先介绍一下科技论文写作中经常使用的一款软件EndNote,这个软件,掌握它的使用方法后会觉得很方便:但 ...
- Flex4 自定义分页组件
自己写的Flex4分页组件,去伪存真,只实现基本的分页功能,数据过滤神马的都不应该是分页组件干的活,有呆毛才有真相: [源代码下载] Flex自从转手给Apache后人气急跌,本人也很捉鸡,尽管Apa ...
- Modifiers
Sometimes it is useful for a function to modify the objects it gets as parameters. In that case, the ...
- Hibernate 常见异常
Hibernate 常见异常net.sf.hibernate.MappingException 当出现net.sf.hibernate.MappingException: Error r ...