Leetcode's "hash list": Valid Sudoku

Source: Internet
Author: User

Topic links

Title Requirements:

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.

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

The solution of this problem is not quite ingenious, so the method used in the following procedure is brute force (violent cracking):

1 classSolution {2  Public:3     intChartoint (Charc)4     {5         CharStr[] = {'1','2','3','4','5','6','7','8','9'};6         intIntstr[] = {1,2,3,4,5,6,7,8,9};7          for(inti =0; I <9; i++)8         {9             if(c = =Str[i])Ten                 returnIntstr[i]; One         } A         return-1; -     } -      the     BOOLIsvalidsudoku (vector<vector<Char>>&Board) { -unordered_map<int,int> hashMap; -          for(inti =1; I <Ten; i++) -Hashmap[i] =0; +         //row by Row -          for(inti =0; I <9; i++) +         { A              for(intK =0; K <Ten; k++) atHASHMAP[K] =0; -              for(intj =0; J <9; J + +) -             { -                 intTMP =Chartoint (Board[i][j]); -                 if(tmp! =-1) -                 { inhashmap[tmp]++; -                     if(Hashmap[tmp] >1) to                         return false; +                 } -             } the         } *         //column by Column $          for(intj =0; J <9; J + +)Panax Notoginseng         { -              for(intK =1; K <Ten; k++) theHASHMAP[K] =0; +              for(inti =0; I <9; i++) A             { the                 intTMP =Chartoint (Board[i][j]); +                 if(tmp! =-1) -                 { $hashmap[tmp]++; $                     if(Hashmap[tmp] >1) -                         return false; -                 } the             } -         }Wuyi         //3*3 boxes by 3*3 boxes the          for(inti =0; I <9; i + =3) -         { Wu              for(intj =0; J <9; J + =3) -             { About                  for(intK =0; K <Ten; k++) $HASHMAP[K] =0; -                  for(intm = i; M < i +3; m++) -                      for(intn = j; N < J +3; n++) -                     { A                         intTMP =Chartoint (Board[m][n]); +                         if(tmp! =-1) the                         { -hashmap[tmp]++; $                             if(Hashmap[tmp] >1) the                                 return false; the                         } the                     } the             } -         } in          the         return true; the     } About};

Leetcode's "hash list": 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.