Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8688eac

Browse files
author
applewjg
committed
ValidSudoku
Change-Id: Ib833015581798bf95f546e0370340732a0e0cb12
1 parentea5ff4f commit8688eac

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

‎ValidSudoku.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Author: King, wangjingui@outlook.com
3+
Date: Dec 20, 2014
4+
Problem: Valid Sudoku
5+
Difficulty: Easy
6+
Source: https://oj.leetcode.com/problems/valid-sudoku/
7+
Notes:
8+
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules (http://sudoku.com.au/TheRules.aspx).
9+
The Sudoku board could be partially filled, where empty cells are filled with the character '.'.
10+
11+
Solution: 1. Traverse the Sudoku only once.
12+
2. Bit manipulation. Use only one bit to represent a number. Space: sizeof(int) * (1+9+9).
13+
*/
14+
publicclassSolution {
15+
publicbooleanisValidSudoku(char[][]board) {
16+
boolean[]used =newboolean[9];
17+
18+
for(inti=0;i<9;i++){
19+
Arrays.fill(used,false);
20+
for(intj =0;j<9;j++){
21+
if(check(board[i][j],used)==false)returnfalse;
22+
}
23+
Arrays.fill(used,false);
24+
for(intj =0;j<9;j++){
25+
if(check(board[j][i],used)==false)returnfalse;
26+
}
27+
}
28+
29+
for(intr =0;r<3;r++){
30+
for(intc =0;c<3;c++){
31+
Arrays.fill(used,false);
32+
for(inti =r*3;i<r*3+3;i++){
33+
for(intj =c*3;j<c*3+3;j++){
34+
if(check(board[i][j],used)==false)returnfalse;
35+
}
36+
}
37+
}
38+
}
39+
returntrue;
40+
}
41+
booleancheck(charch,boolean[]used){
42+
if(ch=='.')returntrue;
43+
if(used[ch-'1'])returnfalse;
44+
used[ch-'1']=true;
45+
returntrue;
46+
}
47+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp