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. asp.net mvc HtmlHelperExt EnumDropDownList

    public static class HtmlHelperExt { public static MvcHtmlString EnumDropDownList<TEnum>(this H ...

  2. 用javascript编写猜拳游戏(函数)

    const readline = require('readline-sync')//引用readline-sync console.log('欢迎进入猜拳游戏'); //电脑随机出拳 let fn ...

  3. 轻量ORM-SqlRepoEx (九)与Dapper共舞

    Dapper就另一个轻量ORM,Dapper及其扩展解决了数据访问端的大部门问题,提供了如数据事务管理.缓存等支持.SqlRepoEx的重点解决了Lambda转换成SQL语句,使SQL使用强类型编写, ...

  4. 竞赛题解 - Broken Tree(CF-758E)

    Broken Tree(CF-758E) - 竞赛题解 贪心复习~(好像暴露了什么算法--) 标签:贪心 / DFS / Codeforces 『题意』 给出一棵以1为根的树,每条边有两个值:p-强度 ...

  5. 剑指Offer_编程题之替换空格

    题目描述 请实现一个函数,将一个字符串中的空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy.

  6. Linux 实时查看进程网络的使用情况

    一行代码实现 linux 指定进程网络的使用情况 pid=4203;count=0;while true;do info2=`sed -n '4,100p' /proc/$pid/net/dev |a ...

  7. vue使用axios调用豆瓣API跨域问题

    最近做了一个vue小demo,使用了豆瓣开源的API,通过ajax请求时需要跨域才能使用.   封面.jpg 一.以下是豆瓣常用的开源接口: 正在热映 :https://api.douban.com/ ...

  8. python3 练习题100例 (五)

    题目五:输入三个整数x,y,z,请把这三个数由小到大输出. #!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 题目五: ...

  9. 清华大学《C++语言程序设计基础》线上课程笔记04---指针

    指针 static int i; static int* ptr = &i; 此处的*表示ptr是指针类型(地址类型),用来存放目标数据的地址 其本身也有地址,所以又指向指针的指针; *前面的 ...

  10. fedora/centos下gcc编译出现gcc: error trying to exec ‘cc1plus’: execvp: No such file or directory

    fedora/centos下gcc编译出现gcc: error trying to exec 'cc1plus': execvp: No such file or directory解决办法 翻译自: ...