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. 30分钟新手git教程

    本文转载自:http://igeekbar.com/igeekbar/post/82.htm Git近些年的火爆程度非同一般,这个版本控制系统被广泛地用在大型开源项目(比如Linux),不同规模的团队 ...

  2. 疯狂java——第一章 java语言概述与开发环境

    J2ME: 主要用于控制移动设备和信息家电等有限存储的设备. J2SE: 整个java技术的核心和基础,它是J2ME和J2EE编程的基础. J2EE: Java技术中应用最广泛的部分,J2EE提供了企 ...

  3. k8s 问题

    kubelet: Orphaned pod "4db449f0-4eaf-11e8-94ab-90b8d042b91a" found, but volume paths are s ...

  4. tf.unstack()、tf.stack()

    tf.unstack 原型: unstack( value, num=None, axis=0, name='unstack' ) 官方解释:https://tensorflow.google.cn/ ...

  5. 140. Word Break II (String; DP,DFS)

    Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...

  6. TZOJ 4839 麦森数(模拟快速幂)

    描述 形如2^P-1的素数称为麦森数,这时P一定也是个素数.但反过来不一定,即如果P是个素数,2^P-1不一定也是素数.到1998年底,人们已找到了37个麦森数.最大的一个是P=3021377,它有9 ...

  7. 【校招面试 之 C/C++】第11题 C++ 纯虚函数

    1.纯虚函数 成员函数的形参后面写上=0,则成员函数为纯虚函数. 纯虚函数声明: virtual 函数类型 函数名 (参数表列) = 0: class Person { virtual void Di ...

  8. [leetcode]173. Binary Search Tree Iterator 二叉搜索树迭代器

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  9. VMware安装centos虚拟机 通过NAT与主机互通并能上网

    1.关于centos虚拟机的安装,我这里就不详细说明了,网上有很多教程,默认你们已经安装好.       (我的环境是centos6.6 x86 最小安装版) 2.右键虚拟主机,选择设置选项. 3.在 ...

  10. mybatis框架入门程序:演示通过mybatis实现数据库的查询操作

    我们现在工程基于的数据库见“https://www.cnblogs.com/wyhluckdog/p/10147754.html”这篇博文. 1.mybatis下载 mybatis的代码由githua ...