[lintcode easy]Valid Sudoku

Valid Sudoku

Determine whether a Sudoku is valid.

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

Example

The following partially filed sudoku is valid.

Note

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

Clarification

What is Sudoku?

////这题很直接

////验证每一行,每一列和每一个小九宫格的数字是否重复

///代码可以优化下,把验证过程单独写一个方法。

class Solution {
    /**
      * @param board: the board
        @return: wether the Sudoku is valid
      */
    public boolean isValidSudoku(char[][] board) {
        int m=board.length;
        int n=board[0].length;   

        if(board.length!=9 || board[0].length!=9) return false;

        boolean isRow=isValidRow(board);
        boolean isCol=isValidCol(board);
        boolean isNine=isValidNine(board);

        if(isRow && isCol && isNine)
        {
            return true;
        }

        else
        return false;

    }

    public boolean isValidRow(char[][] board)
    {

        for(int i=0;i<9;i++)
        {
            boolean[] isValid=new boolean[10];
            int val=0;
            for(int j=0;j<9;j++)
            {
              if(board[i][j]==‘.‘)
              {
                  continue;
              }
              else
              {
                 val=board[i][j]-‘0‘;
              }

              if(isValid[val])
              {
                return false;
              }
              else
              {
                isValid[val]=true;
              }
            }

        }
        return true;
    }

    public boolean isValidCol(char[][] board)
    {

        for(int i=0;i<9;i++)
        {
            boolean[] isValid=new boolean[10];
            int val=0;
            for(int j=0;j<9;j++)
            {
              if(board[j][i]==‘.‘)
              {
                    continue;
              }
              else
              {
              val=board[j][i]-‘0‘;
              }
              if(isValid[val])
              {
                return false;
              }
              else
              {
                isValid[val]=true;
              }
            }
        }
        return true;
    }

    public boolean isValidNine(char[][] board)
    {
        for(int i=0;i<9;i+=3)
        {
            for(int j=0;j<9;j+=3)
            {
                boolean[] isValid=new boolean[10];
                int val=0;
                for(int a=0;a<3;a++)
                {
                    for(int b=0;b<3;b++)
                    {

                        if(board[i+a][j+b]==‘.‘)
                        {
                            continue;
                        }
                        else
                        {
                            val=board[i+a][j+b]-‘0‘;
                        }

                        if(isValid[val])
                       {
                           return false;
                        }
                        else
                       {
                           isValid[val]=true;
                       }
                    }
                }
            }
        }
        return true;
    }
};
时间: 2024-08-05 07:07:29

[lintcode easy]Valid Sudoku的相关文章

[lintcode easy]Valid Palindrome

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. Example "A man, a plan, a canal: Panama" is a palindrome. "race a car" is not a palindrome. Note Have you consider that the s

[lintcode easy]Valid Parentheses

Valid Parentheses 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 "(]

[LeetCode] 036. Valid Sudoku (Easy) (C++)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 036. Valid Sudoku (Easy) 链接: 题目:https://leetcode.com/problems/valid-sudoku/ 代码(github):https://github.com/illuz/leetcode 题意: 判断一个数独是否有效. 有效的数独不强求有解. 分析: 只要同一行

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 vali

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

[LeetCode]Valid Sudoku

检测数独是否合格. 思路: 填充一遍就知道是否合格. 基本暴力搜索的思想. 1 /*************************************************************************************************** 2 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. 3 The Sudoku board could be parti

leetcode笔记:Valid Sudoku

一.题目描述 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules: http://sudoku.com.au/TheRules.aspx . The Sudoku board could be partially filled, where empty cells are filled with the character '.'. The following figure: A partially f

【LeetCode】Valid Sudoku

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. Note:A valid Sudoku boar

【LeetCode】36 - Valid Sudoku

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.(http://sudoku.com.au/TheRules.aspx) The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. N