Description

Determine whether a Sudoku is valid.

The Sudoku board could be partially filled, where empty cells are filled with the character ..

A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.

Example

The following partially filed sudoku is valid.

解题:判断数独是否有效。如果每一行、每一列、每一个小方块里的数不重复,就算有效。当然,前提必须是1~9之间。此题空白部分是用“ . ”表示的,要注意一下。具体做法为,检查行、列、方块,把这些数拿出来,放在一个一维数组中,判断这个一维数组里的数,满不满足相应的条件。代码如下:

public class Solution {
/**
* @param board: the board
* @return: whether the Sudoku is valid
*/
public boolean isValidSudoku(char[][] board) {
// write your code here
char[]temp = new char[9];
for(int i = 0; i < 9; i++){
//检查行
for(int j = 0; j < 9; j++){
temp[j] = board[i][j];
}
if(judge(temp) == false){
return false;
}
//检查列
for(int j = 0; j < 9; j++){
temp[j] = board[j][i];
}
if(judge(temp) == false){
return false;
}
}
//小格子
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
int b = 0;
for(int k = 0 + i * 3; k < 3 + i * 3; k++){
for(int m = 0 + j * 3; m < 3 + j * 3; m++){
temp[b++] = board[k][m];
}
}
if(judge(temp) == false)
return false; }
}
return true;
}
public boolean judge(char[]temp){
for(int i =0; i < 9; i++){
if(temp[i] > '9' || temp[i] < '1' ){
if(temp[i] != '.')
return false;
}
}
for(int i = 1; i < 9; i++){
for(int j = 0; j < i; j++){
if(temp[i] == temp[j] ){
if(temp[i] != '.')
return false;
}
}
}
return true;
}
}

389. Valid Sudoku【LintCode java】的更多相关文章

  1. 423. Valid Parentheses【LintCode java】

    Description Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine ...

  2. 415. Valid Palindrome【LintCode java】

    Description Given a string, determine if it is a palindrome, considering only alphanumeric character ...

  3. 376. Binary Tree Path Sum【LintCode java】

    Description Given a binary tree, find all paths that sum of the nodes in the path equals to a given ...

  4. 372. Delete Node in a Linked List【LintCode java】

    Description Implement an algorithm to delete a node in the middle of a singly linked list, given onl ...

  5. 451. Swap Nodes in Pairs【LintCode java】

    Description Given a linked list, swap every two adjacent nodes and return its head. Example Given 1- ...

  6. 445. Cosine Similarity【LintCode java】

    Description Cosine similarity is a measure of similarity between two vectors of an inner product spa ...

  7. 433. Number of Islands【LintCode java】

    Description Given a boolean 2D matrix, 0 is represented as the sea, 1 is represented as the island. ...

  8. 422. Length of Last Word【LintCode java】

    Description Given a string s consists of upper/lower-case alphabets and empty space characters ' ', ...

  9. 420. Count and Say【LintCode java】

    Description The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, ...

随机推荐

  1. private、protected、public和internal的区别

    private是完全私有的,只有在类自己里面可以调用,在类的外部和子类都不能调用,子类也不能继承父类的private的属性和方法. protected虽然可以被外界看到,但外界却不能调用,只有自己及自 ...

  2. Unity 游戏框架搭建 2018 (二) 单例的模板与最佳实践

    Unity 游戏框架搭建 2018 (二) 单例的模板与最佳实践 背景 很多开发者或者有经验的老手都会建议尽量不要用单例模式,这是有原因的. 单例模式是设计模式中最简单的也是大家通常最先接触的一种设计 ...

  3. oracle计算某个表中数据所占表空间的比例

    要求计算某个表所占表空间的大小,网上查了些资料用到了oracle的3个视图.具体sql如下 select segment_name as tablename, round(bytes / (selec ...

  4. Jquery知识点总结(一)

    JQuery遍历1 传统的for   2 通过each对象调用callback函数 callback回调函数 /*    * JQ提供的技术,实现遍历    * JQ对象函数调用 each(参数 ca ...

  5. 使用JDK开发WebServrice案例

    使用JDK开发WebServrice案例: 一.开发WebService服务器端 第一步:创建Java工程 ,创建相应的包(服务端)使用JDK开发(1.6以上版本) 第二步:建一个接口WebServi ...

  6. MySQL---事务、函数

    事务 事务用于将某些操作的多个SQL作为原子性操作,一旦有某一个出现错误,即可回滚到原来的状态,从而保证数据库数据完整性. delimiter \\ create PROCEDURE p1( OUT ...

  7. Vue清除所有JS定时器

    Vue清除所有JS定时器 在webpack + vue 的项目中如何在页面跳转的时候清除所有的定时器 JS定时器会有一个返回值(数字),通过这个返回值我们可以找到这个定时器 在vue项目中可以使用路由 ...

  8. C# WebBrowser获取指定字符串的坐标

    public void FindKeyWord(string keyWord) { WebBrowser wb = new WebBrowser(); foreach (HtmlElement ite ...

  9. day31 进程和其他方法,锁,队列

    1.进程的其他方法: 首先引入模块: import os from multiprocessing import Process p = Process(target=f,) 进程的id:  p.pi ...

  10. C语言中结构体的访问方法解读

    在C语言中,对结构体的访问一般有两种常规方式:"."访问和"->"访问.那么两者有什么区别呢?对C语言有一定了解的同学应该知道,我们新建一个结构体的时候, ...