389. Valid Sudoku "Lintcode java"

Source: Internet
Author: User
Tags lintcode

Description

Determine whether a Sudoku is valid.

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

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

Clarification

What's Sudoku ?

    • Http://sudoku.com.au/TheRules.aspx
    • https://zh.wikipedia.org/wiki/Sudoku
    • Https://en.wikipedia.org/wiki/Sudoku
    • Http://baike.baidu.com/subview/961/10842669.htm
Example

The following partially filed Sudoku is valid.

Problem solving: Judging whether Sudoku is effective. If the number in each row, column, or small square is not duplicated, it is valid. Of course, the premise must be between 1~9. The blank part of the question is used. "To be careful," he said. To do this, check the rows, columns, and squares, take these numbers out, put them in a one-dimensional array, and judge the number of the one-dimensional array, full of the corresponding conditions. The code is as follows:

 Public classSolution {/**     * @paramboard:the Board *@return: Whether the Sudoku is valid*/     Public BooleanIsvalidsudoku (Char[] board) {        //Write your code here        Char[]temp =New Char[9];  for(inti = 0; I < 9; i++){            //Check Row             for(intj = 0; J < 9; J + +) {Temp[j]=Board[i][j]; }            if(Judge (temp) = =false){                return false; }            //Check Column             for(intj = 0; J < 9; J + +) {Temp[j]=Board[j][i]; }            if(Judge (temp) = =false){                return false; }        }        //Small Lattice         for(inti = 0; I < 3; i++){             for(intj = 0; J < 3; J + +){                intb = 0;  for(intK = 0 + i * 3; K < 3 + i * 3; k++){                     for(intm = 0 + J * 3; M < 3 + J * 3; m++) {temp[b++] =Board[k][m]; }                }                if(Judge (temp) = =false)                return false; }        }        return true; }     Public BooleanJudgeChar[]temp] {         for(inti = 0; I < 9; i++){            if(Temp[i] > ' 9 ' | | temp[i] < ' 1 ' ){                if(Temp[i]! = '. ')                   return false; }        }         for(inti = 1; I < 9; i++){             for(intj = 0; J < I; J + +){                if(Temp[i] = =Temp[j]) {                    if(Temp[i]! = '. ')                        return false; }            }        }        return true; }}

389. Valid Sudoku "Lintcode java"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.