389. Valid Sudoku【LintCode java】

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.

Clarification

What is Sudoku?

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;
    }
}

原文地址:https://www.cnblogs.com/phdeblog/p/9313231.html

时间: 2024-11-07 20:48:56

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

423. Valid Parentheses【LintCode java】

Description Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. Example The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]"

245. Subtree【LintCode java】

Description You have two very large binary trees: T1, with millions of nodes, and T2, with hundreds of nodes. Create an algorithm to decide if T2 is a subtree of T1. A tree T2 is a subtree of T1 if there exists a node n in T1 such that the subtree of

365. Count 1 in Binary【LintCode java】

Description Count how many 1 in binary representation of a 32-bit integer. Example Given 32, return 1 Given 5, return 2 Given 1023, return 9 Challenge If the integer is n bits with m 1 bits. Can you do it in O(m) time? 解题:很简单,但是要考虑范围的问题.代码如下: public

LeetCode 36 Valid Sudoku (C,C++,Java,Python)

Problem: 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. Note: A valid Sudoku board (

158. Valid Anagram【LintCode by java】

Description Write a method anagram(s,t) to decide if two strings are anagrams or not. Clarification What is Anagram? Two strings are anagram if they can be the same after change the order of characters. Example Given s = "abcd", t = "dcab&q

【Effective Java】8、优先考虑类型安全的异构容器

有的时候我们一个容器只有一个类型或几个类型并不能满足我们的要求,比如set中存放的元素类型都是同一种,map也就指定的两种 这里我们可以将键进行参数化,而不是将容器参数化,也就是我们可以给容器传一个键的类型,然后value用来放对应的实例,这样就可以存放多个不同的类型了 如: package cn.xf.cp.ch02.item29; import java.util.HashMap; import java.util.Map; public class ManyTypeClass { //一个

【Effective Java】7、优先考虑泛型方法

package cn.xf.cp.ch02.item27; import java.util.HashSet; import java.util.Set; public class Union { /** * 这个方法就会有警告 * @param s1 * @param s2 * @return */ public static Set union1(Set s1, Set s2) { Set result = new HashSet(s1); result.addAll(s2); return

【Effective Java】5、覆盖equals时总要覆盖hashcode

package cn.xf.cp.ch02.item9; import java.util.HashMap; import java.util.Map; public class PhoneNumber { private final short areaCode; private final short prefix; private final short lineNumber; public PhoneNumber(int areaCode, int prefix, int lineNum

【Simple Java】HashMap常用方法

当需要对元素进行计数时,HashMap非常有用,如下例子,统计一个字符串中每个字符出现的次数: package simplejava; import java.util.HashMap; import java.util.Map.Entry; public class Q12 { public static void main(String[] args) { HashMap<Integer, Integer> countMap = new HashMap<Integer, Intege