[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 ...
随机推荐
- 编写webpy程序,iep 报错,ulipad 运行正确
在web.py编程中,使用模板文件时,iep下会报错.ulipad不会报错. 用python 运行不报错. 在寻找答案.初步估计是iep的python运行环境有问题. 如图:
- PHP 连接 MSSQL
1 设置 2 php 代码: <?php header('Content-Type:text/html; charset=GBK'); define('DB_HOST','localhost') ...
- esriSRGeoCSType Constants
ArcGIS Developer Help (Geometry) esriSRGeoCSType Constants See Also esriSRGeoCS2Type Constants ...
- 百度之星IP聚合(水题map&字符处理)
虽然题目停水的,但是好像字符处理运用的还比较合适 Problem Description 当今世界,网络已经无处不在了,小度熊由于犯了错误,当上了度度公司的网络管理员,他手上有大量的 IP列表,小度熊 ...
- MySQL数据库优化技术概述
对于一个以数据库为中心的应用,数据库的优化直接影响到程序的性能,因此数据库性能至关重要.一般来说,要保证数据库的效率,要做好以下几个方面的工作: 1. 数据库表设计: 表的设计合理化(符合3NF): ...
- 转载:Windows Phone 8.1 投影我的屏幕使用教程
原文地址:http://livesino.net/archives/6851.live 更新了软件的下载地址. Windows Phone 8.1 新功能投影我的屏幕(Project My Scree ...
- 防范ARP网关欺骗, ip mac双向绑定脚本
客户局域网内的一台数据库服务器, 重新安装操作系统后,不能上网了,ping网关192.168.0.1出现在800多ms的响应时间,还会超时丢包,检查了ip,路由配置,都没有问题.通过IE打开路由器管理 ...
- PAT1030. Travel Plan
//晴神模板,dij+dfs,貌似最近几年PAT的的图论大体都这么干的,现在还在套用摸板阶段....估计把这及格图论题题搞完,dij,dfs,并查集就掌握差不多了(模板还差不多)为何bfs能自己干出来 ...
- 开源项目:libbpg
1 ubuntu下编译libbpg(编译机器64bit) 安装cmake,libpng,yasm,gcc,g++ cmake版本最低为2.8.8,安装完毕后使用cmake --version查看是否安 ...
- ios NSString常见的字符串操作 分割 查找
1.NSString *str = [[NSString alloc]init]; //简单粗暴,基本用不到 2.NSString *str = [[NSString alloc]initWi ...