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.

java code :

public class Solution {
public boolean isValidSudoku(char[][] board) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
HashSet<Character> hash = new HashSet<Character>();
if(board == null)
return true;
for(int i = 0; i < board.length; i++)
{
hash.clear();
for(int j = 0; j < board[0].length; j++)
{
if(board[i][j] == '.')
continue;
if(!(board[i][j] >= '1' && board[i][j] <= '9'))
return false;
if(hash.isEmpty() || !hash.contains(board[i][j]))
hash.add(board[i][j]);
else return false;
}
}
hash.clear();
for(int i = 0; i < board[0].length; i++)
{
hash.clear();
for(int j = 0; j < board.length; j++)
{
if(board[j][i] == '.')
continue;
if(!(board[j][i] >= '1' && board[j][i] <= '9'))
return false;
if(hash.isEmpty() || !hash.contains(board[j][i]))
hash.add(board[j][i]);
else return false;
}
}
hash.clear(); for(int i = 0; i < 9; i += 3)
{ for(int j = 0; j < 9; j += 3)
{
hash.clear();
for(int row = 0; row < 3; row++)
{
for(int col = 0; col < 3; col++)
{
if(board[i+row][j+col] == '.')
continue;
if(!(board[i+row][j+col] >= '1' && board[i+row][j+col] <= '9'))
return false;
if(hash.isEmpty() || !hash.contains(board[i+row][j+col]))
hash.add(board[i+row][j+col]);
else return false;
}
}
}
}
hash = null;
return true;
}
}

【LeetCode】 Valid Sudoku的更多相关文章

  1. 【leetcode】Valid Sudoku

    题目简述: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board cou ...

  2. 【leetcode】Valid Sudoku (easy)

    题目:就是判断已有的数字是否冲突无效,若无效返回flase 有效返回true 不要求sudo可解 用了char型的数字,并且空格用‘.'来表示的. 思路:只要分别判断横向 竖向 3*3小块中的数字是否 ...

  3. 【Leetcode】【Easy】Valid Sudoku

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...

  4. 【LeetCode】37. Sudoku Solver

    Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...

  5. 【leetcode】Valid Palindrome

    题目简述: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...

  6. 【leetcode】Valid Parentheses

    题目简述: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if th ...

  7. 【leetcode】Valid Number

    Valid Number Validate if a given string is numeric. Some examples:"0" => true" 0.1 ...

  8. 【题解】【字符串】【Leetcode】Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  9. 【LeetCode】- Valid Palindrome(右回文)

    [ 问题: ] Given a string, determine if it is a palindrome, considering only alphanumeric characters an ...

随机推荐

  1. RNA-seq 安装 fastaqc,tophat,cuffilnks,hisat2

    ------------------------------------------ 安装fastqc------------------------------------------------- ...

  2. Maven常用命令及Eclipse应用

    一般来说,github上大多的java项目都是使用maven,ant等进行构建的.由于之前没有使用过maven,因此这几天对maven进行了简单的学习.古话说:“温故而知新”,一些命令长时间不使用都会 ...

  3. Spring工作原理与单例

    最近看到spring管理的bean为单例的,当它与web容器整合的时候始终搞不太清除,就网上搜索写资料, Tomcat与多线程, servlet是多线程执行的,多线程是容器提供的能力. servlet ...

  4. 强关联二维材料1T—TaS2晶体

    我校物理系张远波教授课题组通过一种新的实验方法——可控电荷插层,实现了对强关联二维材料1T—TaS2晶体相变的全面研究.1月26日,相关研究论文Gate-tunable phase transitio ...

  5. Linux跑火车,提升趣味性

     實現跑火車[可陶冶情操,愉悦心情]##下载yum源[root@localhost ~]# wget http://mirror.centos.org/centos/7/extras/x86_64/P ...

  6. 识别名人 · Find the Celebrity

    [抄题]: 假设你和 n 个人在一个聚会中(标记为 0 到 n - 1),其中可能存在一个名人.名人的定义是所有其他 n - 1 人都认识他/她,但他/她不知道任何一个.现在你想要找出这个名人是谁或者 ...

  7. https://www.w3.org/

    W3C   W3C By Region All Australia Österreich (Austria) België (Belgium) Botswana Brasil (Brazil) 中国 ...

  8. MongoDb进阶实践之四 MongoDB查询命令详述

    一.引言 上一篇文章我们已经介绍了MongoDB数据库的最基本操作,包括数据库的创建.使用和删除数据库,文档的操作也涉及到了文档的创建.删除.更新和查询,当然也包括集合的创建.重命名和删除.有了这些基 ...

  9. ILSpy 反编译.NET

    ILSpy 是一个开源的.NET反编译工具,简洁强大易用是它的特征.在绝大多数情况下,它都能很好的完成你对未知程序集内部代码的探索.

  10. process_进程池

    from multiprocessing import Poolimport os,timeimport multiprocessingdef func(msg): print("msg:& ...