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. hive 显示分区

    显示某一张表的分区值 show partitions table_name;

  2. javascript中的二维数组

    要创建一个二位数组我们脑子里第一个出现的就是 var arr=[][]; 但是在javascript这样是会报错的,要在javascrip中创建一个二位数组对象方法如下 方法一     直接把数组写出 ...

  3. 基于mysql全文索引的深入理解

    最近要使用mysql的全文索引,一直没能成功,一个是只有MyISAM引擎支持,创建表时需要指定,而是需要对my.ini进行配置. 前言:本文简单讲述全文索引的应用实例,MYSQL演示版本5.5.24. ...

  4. 微信公众平台开发之基于百度 BAE3.0 的开发环境搭建(采用 Baidu Eclipse)

    3.通过 SVN 检入工程     在 bae 上的应用添加部署成功后,如图 7     点击“点击查看”按钮,会打开一个新页面,页面上会打印 “hello world” ,这是因为我们的应用包含有示 ...

  5. leetcode 196. Delete Duplicate Emails

    # 慢,内连接delete p1 from Person p1, Person p2 where p1.Email=p2.Email and p1.Id>p2.Id delete from Pe ...

  6. log4j每天,每小时产生一日志文件

    log4j每天,每小时产生一日志文件 2016年08月05日 14:14:33 阅读数:6254 一.之前的文章中有log4j的相关配置以及属性的介绍,下面我们先把配置列出来:   log4j.roo ...

  7. 移动端 输入框 如果类型是number,用户也可以输入汉字和字母

    <input type="number" id="input-age" placeholder="请输入你的年龄" /> //i ...

  8. PHP+Gtk实例(求24点)

    作者: Laruence(   ) 本文地址: http://www.laruence.com/2009/05/26/871.html 转载请注明出处 最近要安排我为BIT提供的<PHP高级应用 ...

  9. linux下给php安装memcached及memcache扩展(转)

    http://kimi.it/257.html (另外的方法)linux安装memcached及memcache扩展一.安装libevent函数库下载地址:http://libevent.org默认被 ...

  10. 建表Table

    Sstudent表   学  号    Sno  姓  名   Sname   性  别    Ssex     年  龄      Sage   所 在 系    Sdept   200215121 ...