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. 代码重构:用工厂+策略模式优化过多的if else代码块

    最近在工作中优化了一段冗余的if else代码块,感觉对设计模式的理解和运用很有帮助,所以分享出来.鉴于原代码会涉及到公司的隐私,因此就不贴出来了.下面以更加通俗易懂的案例来解析. 假如写一个针对员工 ...

  2. conductor Kitchensink示例

    一个示例的厨房工作流程,演示了所有模式构造的使用. 定义 { "name": "kitchensink", "description": & ...

  3. pip批量更新安装的包

    ------------------pip批量更新库-------------------- 1)查看过期的库 pip list --outdated  更新单一的库: pip install --u ...

  4. date.getTime()

    Date date = new Date(); System.out.println(date.getTime()); 输出结果是1210745780625 编译时间当时时间大概是2008年5.14好 ...

  5. Maven(一)——Maven入门

    一.Maven的基本概念 Maven(翻译为"专家","内行")是跨平台的项目管理工具.主要服务于基于Java平台的项目构建,依赖管理和项目信息管理. 1.1. ...

  6. (mac环境)Appium安装了client包,但是提示no module named appium

    背景 mac环境,使用pip install Appium-Python-Client已经安装了client包   问题 import appium,提示no module named appium ...

  7. [leetcode]283. Move Zeroes移零

    Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...

  8. php下的原生ajax请求

    浏览器中为我们提供了一个JS对象XMLHttpRequet,它可以帮助我们发送HTTP请求,并接受服务端的响应. 意味着我们的浏览器不提交,通过JS就可以请求服务器.   ajax(Asynchron ...

  9. maven项目工程目录约定

    使用maven创建的工程我们称它为maven工程,maven工程具有一定的目录规范,如下: src/main/java —— 存放项目的.java文件 src/main/resources —— 存放 ...

  10. 【转】HttpApplication的认识与加深理解

    原文:http://www.cnblogs.com/whtydn/archive/2009/10/16/1584584.html HttpApplication对象是经由HttpApplication ...