【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.
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的更多相关文章
- 【leetcode】Valid Sudoku
题目简述: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board cou ...
- 【leetcode】Valid Sudoku (easy)
题目:就是判断已有的数字是否冲突无效,若无效返回flase 有效返回true 不要求sudo可解 用了char型的数字,并且空格用‘.'来表示的. 思路:只要分别判断横向 竖向 3*3小块中的数字是否 ...
- 【Leetcode】【Easy】Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
- 【LeetCode】37. Sudoku Solver
Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...
- 【leetcode】Valid Palindrome
题目简述: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...
- 【leetcode】Valid Parentheses
题目简述: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if th ...
- 【leetcode】Valid Number
Valid Number Validate if a given string is numeric. Some examples:"0" => true" 0.1 ...
- 【题解】【字符串】【Leetcode】Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- 【LeetCode】- Valid Palindrome(右回文)
[ 问题: ] Given a string, determine if it is a palindrome, considering only alphanumeric characters an ...
随机推荐
- pyplot绘图区域
pyplot绘图区域 Matplotlib图像组成 matplotlib中,整个图像为一个Figure对象,与用户交互的整个窗口 Figure对象中包含一个或多个Axes(ax)子对象,每个ax子对象 ...
- 疯狂JAVA——第二章 理解面向对象
面向对象的三大特征:继承.封装和多态 面向对象的方式实际上由OOA(面向对象分析).OOD(面向对象设计)和OOP(面相对象编程)三个部分组成,其中OOA和OOD的结构需要用一个描述方式来描述并记录, ...
- linux替换字符串的几种方法
1. 基本替换:s/str1/str2/ 替换当前行第一个str1为str2:s/str1/str2/g 替换当前行所有str1为str2:n,$s/str1/str2/ 替换第 n 行开始到最后一行 ...
- distinct top執行順序
select distinct top 3 from table; 先distinct后top
- testlink问题--linux环境下
搭建testlink 时出现问题,相关解决办法: 1.Maximum Session Idle Time before Timeout 修改php.ini文件,修改成session.gc_maxlif ...
- C语言实现24点程序
一.简介 本程序的思想和算法来自于C语言教材后的实训项目,程序通过用户输入四个整数计算出能够通过加减乘除得到数字24的所有表达式,程序的设计有别于一般通过穷举实现的方式,效率得到提高.算法介绍如下: ...
- ubuntu的文本界面修改字体大小
使用命令: dpkg-reconfigure console-setup
- Python float() 函数
Python float() 函数 Python 内置函数 描述 float() 函数用于将整数和字符串转换成浮点数. 语法 float()方法语法: class float([x]) 参数 x - ...
- 【c++】多层次继承类对象的构造函数参数的传递方法
#include <iostream.h> //基类CBase class CBase { int a; public: CBase(int na) { a=na; cout<< ...
- python的select服务端的代码和客户端的代码
服务端的代码 import socket import queue import select ip_bind = ("127.0.0.1",9000) message_queue ...