Leetcode#36valid Sudoku

Source: Internet
Author: User

Valid Sudoku

Total Accepted: 33785 Total Submissions: 123877 My Submissions

Question Solution


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 ‘.‘ .

650) this.width=650; "Src=" http://upload.wikimedia.org/wikipedia/commons/thumb/f/ff/Sudoku-by-L2G-20050714.svg/ 250px-sudoku-by-l2g-20050714.svg.png "style=" border:0px;vertical-align:middle; "alt=" 250px-sudoku-by-l2g-20050714.svg.png "/>

A partially filled sudoku which is valid.



Analysis: Each element in each row can be empty, but the 0~9 number only appears once;

Each element in each column can be empty, but the 0~9 number only appears once;

Each element in a small nine can be empty, but the 0~9 number only appears once;


public class Solution {

Boolean IsV (int xs, int xe, int ys, int ye, char[][] board)

{

Map<character,integer> z=new hashmap<character,integer> ();

for (int i=xs;i<=xe;i++)

for (int j=ys;j<=ye;j++)

if (board[i][j]>= ' 0 ' &&board[i][j]<= ' 9 ')

if (Z.get (Board[i][j])!=null)

return false;

Else

Z.put (board[i][j],1);

return true;

}

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

Map<character,integer> x;

Map<character,integer> y;

for (int i=0;i<9;i++)

{

X=new hashmap<character,integer> ();

Y=new hashmap<character,integer> ();

for (int j=0;j<9;j++)

{

if (board[i][j]>= ' 0 ' &&board[i][j]<= ' 9 ')

if (X.get (Board[i][j])!=null)

return false;

Else

X.put (board[i][j],1);

if (board[j][i]>= ' 0 ' &&board[j][i]<= ' 9 ')

if (Y.get (Board[j][i])!=null)

return false;

Else

Y.put (board[j][i],1);

}

X.clear ();

Y.clear ();

}

if (IsV (0,2,0,2,board) &&isv (3,5,0,2,board) &&isv (6,8,0,2,board) &&ISV (0,2,3,5,board) & &ISV (3,5,3,5,board) &&isv (6,8,3,5,board) &&isv (0,2,6,8,board) &&ISV (3,5,6,8,board) &&ISV (6,8,6,8,board))

return true;

Else

return false;

}

}


Leetcode#36valid 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.