[Leedcode 36] Valid Sudoku

Source: Internet
Author: User

Determine if a Sudoku is valid, according To:sudoku puzzles-the Rules.

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

A partially filled sudoku which is valid.

 Public classSolution { Public BooleanIsvalidsudoku (Char[] board) {        //The idea: first each row of each column to determine whether there is a number of repetitions, the problem with the help of ArrayList, pay attention to Clear,contains,add and other methods, when the board element is not '. ' Only when the judgment is made.//judge the rows and columns to determine the block of each 3*3. Note the way of traversal, four for loops        intlen=board.length; List<Character> row=NewArraylist<character>(); List<Character> col=NewArraylist<character>();  for(inti=0;i<len;i++) {row.clear ();            Col.clear ();  for(intj=0;j<len;j++){                                CharR=Board[i][j]; if(r!= '. ')){                    if(Row.contains (R))return false; Else{Row.add (R); }                }                CharC=Board[j][i]; if(c!= '. ')){                    if(Col.contains (c))return false; Else{Col.add (c); } }}} ArrayList<Character> block=NewArraylist<character>();  for(intK=0;K&LT;LEN;K=K+3) {//K represents the row of the block, and S represents the column of the block. Each block is then traversed             for(intS=0;s<len;s=s+3) {block.clear ();  for(inti=0;i<3;i++){                     for(intj=0;j<3;j++){                      Charb=board[i+k][j+S]; if(b!= '. ')){                            if(Block.contains (b))return false; Else{Block.add (b); }                        }                      }                }            }        }                      return true; }}

[Leedcode 36] Valid Sudoku

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.