LeetCode——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 '.'.
![]()
A partially filled sudoku which is valid.
Note:
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
原题链接:https://oj.leetcode.com/problems/valid-sudoku/
依照数独的规则。一行、一列、对角线、9个小格中不出现同一个数字。
import java.util.ArrayList;
import java.util.List; public class ValidSudoku {
public boolean isValidSudoku(char[][] board) {
for(int i=0;i<9;i++){
List<Character> list = new ArrayList<Character>();
for(int j=0;j<9;j++)
list.add(board[i][j]);
if(!isValid(list))
return false;
}
for(int i=0;i<9;i++){
List<Character> list = new ArrayList<Character>();
for(int j=0;j<9;j++)
list.add(board[j][i]);
if(!isValid(list))
return false;
}
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
List<Character> list = new ArrayList<Character>();
for(int k=0;k<3;k++){
for(int l=0;l<3;l++){
list.add(board[i*3+k][j*3+l]);
}
}
if(!isValid(list))
return false;
}
}
return true;
}
private boolean isValid(List<Character> list){
for(Character ch : list){
if(ch != '.')
if(list.indexOf(ch) != list.lastIndexOf(ch))
return false;
}
return true;
}
}
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,Sudoku Solver(数独游戏)
Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...
- LeetCode: Valid Sudoku 解题报告
Valid SudokuDetermine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku boa ...
- [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 | Valid Sudoku & Sudoku Solver
判断valid,没有更好的方法,只能brute force. class Solution { public: bool isValidSudoku(vector<vector<char& ...
- leetcode Valid Sudoku python
#数独(すうどく,Sūdoku)是一种运用纸.笔进行演算的逻辑游戏.玩家需要根据9×9盘面上的已知数字,推理出所有剩余空格的数字,并满足每一行.每一列.每一个粗线宫内的数字均含1-9,不重复.#数独盘 ...
- [LeetCode]Valid Sudoku解题记录
这道题考查对二维数组的处理,哈希表. 1.最自然的方法就是分别看每一个数是否符合三个规则.所以就须要对应的数据结构来 记录这些信息,判定是否存在.显然最先想到用哈希表. 2.学会把问题抽象成一个个的子 ...
- LeetCode:36. Valid Sudoku,数独是否有效
LeetCode:36. Valid Sudoku,数独是否有效 : 题目: LeetCode:36. Valid Sudoku 描述: Determine if a Sudoku is valid, ...
随机推荐
- android 实现代码关机
开始从网上搜索,通过发action的方式实现,不过一直没有成功. Intent intent = new Intent(); intent.setAction(Intent.ACTION_SHUT ...
- 手动配置S2SH三大框架报错(二)
十二月 08, 2013 9:34:39 下午 org.apache.catalina.core.AprLifecycleListener init 严重: An incompatible versi ...
- meminfo,df,
yx100:root@yxRouter:/# cat /proc/meminfoMemTotal: 126584 kBMemFree: 103156 kBBuffer ...
- Mysql 官方Memcached 插件初步试用感受 - schweigen - ITeye技术网站
Mysql 官方Memcached 插件初步试用感受 - schweigen - ITeye技术网站 Mysql 官方Memcached 插件初步试用感受
- maven项目配置Project Facets时further configuration available不出来问题
如果下边的 further configuration available不出来 把Dynamic web module 去掉勾选,应用与项目,然后再点开项目的properties,再选中Dynami ...
- c coding style之学习篇
1. 使用do-while结构去避免潜在的内存泄漏问题. do { p1 = malloc(10); if (null == p1) { break; ...
- 深刻:截获windows的消息并分析实例(DefWindowProc),以WM_NCHITTEST举例(Windows下每一个鼠标消息都是由 WM_NCHITTEST 消息产生的,这个消息的参数包含了鼠标位置的信息)
1,回调函数工作机制 回调函数由操作系统自动调用,回调函数的返回值当然也是返回给操作系统了. 2,截获操作系统发出的消息,截获到后,将另外一个消息返回给操作系统,已达到欺骗操作系统的目的. 下面还是以 ...
- 开源搜索引擎评估:lucene sphinx elasticsearch
开源搜索引擎评估:lucene sphinx elasticsearch 开源搜索引擎程序有3大类 lucene系,java开发,包括solr和elasticsearch sphinx,c++开发,简 ...
- nohup sort -k1 -n -t$'\t' ./bigfile.16 -o./test/bigfile.16.ok &
nohup sort -k1 -n -t$'\t' ./bigfile.16 -o./test/bigfile.16.ok &
- dtach-linux-分离功能-小工具 - 点点滴滴 Linux | 点点滴滴 Linux
dtach-linux-分离功能-小工具 - 点点滴滴 Linux | 点点滴滴 Linux dtach-linux-分离功能-小工具 2013年05月20日 ⁄ Linux工具 ⁄ 共 1775字 ...