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. C# 类中的静态字段始终继承自基类

    我们试想一下现在有一个类Parent,它有一个static的int类型字段number,然后如果类Parent有三个子类Child01.Child02和Child03,那么改变Parent.numbe ...

  2. 阿里云云服务器Windows Server 2012 R2无法安装IIS等组件的解决办法

    Windows Server2012 R2数据中心版 不管安装什么组件,都显示存储空间不足,无法应用命令,错误代码0x80070008. 最终确认是服务器配置过低的原因,因为这个型号是低级别的配置,1 ...

  3. unittest单元测试框架之unittest案例(二)

    1.待测方法: # 加法,返回 a+b 的值 def add(a,b): return a+b # 减法,返回 a-b 的值 def minus(a,b): return a-b # 乘法,返回 a* ...

  4. 个人开源Git地址

    开源Git地址 序号 Git地址 描述 1 https://github.com/winds-june 各种源码.直接调用的jar包          2    

  5. Javascript混淆与解混淆的那些事儿

    像软件加密与解密一样,javascript的混淆与解混淆同属于同一个范畴.道高一尺,魔高一丈.没有永恒的黑,也没有永恒的白.一切都是资本市场驱动行为,现在都流行你能为人解决什么问题,这个概念.那么市场 ...

  6. Mysql 几种常见的插入 Insert into,Replace Into,Insert ignore

    简要说下三者的区别:insert into 最普遍的插入,如果表中存在主键相同的数据,执行会报错. replace into 如果表中存在主键相同的数据则根据主键修改当前主键的数据,反之则插入(存在就 ...

  7. AML与PIO整合问题

    要想把PIO引擎封装成AML组件,面临如下问题(逐渐补充): 1)版本不兼容 内容项 AML PIO 选型 兼容? JDK 1.7 1.8 1.8 是 SPARK 1.6.1 2.1.1     HA ...

  8. Apache 错误:httpd: Could not open configuration file

    神奇的事件,折磨我 电脑关机重启了一下关机之前正常的状态没有任何的异常出现,过了一会开机准备工作.神奇的事情tmd出现了!!!! 打开phpstudy 启动... 嗯?apache亮红报错?? 第一反 ...

  9. PHP中如何对二维数组按某个键值进行排序

    $arr=[     array(         'name'=>'张三',         'age'=>28     ),     array(         'name'=> ...

  10. yii学习笔记(3),自定义全局工具函数

    在平时开发是经常需要打印数据来调试 常见的打印方式有print_r和var_dump,但是这样打印出来格式很乱不好浏览 在打印函数前后加上<pre></pre>就可以将内容原样 ...