Created
January 18, 2021 07:54
-
-
Save mumunuu/493b471b6b55e8b9a21c854889ac79b0 to your computer and use it in GitHub Desktop.
algorithm(leetcode) Valid Sudoku
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func isValidSudoku(board [][]byte) bool { | |
//체크할 빈 배열을 생성 | |
col, row, blk := make2DArray(), make2DArray(), make2DArray() | |
//해당 숫자가, 해당 포지션에 지나간적이 있는지 체크하면 된다. | |
for i := 0; i < 9; i++ { | |
for j := 0; j < 9; j++ { //값을 모두 체크 | |
if board[i][j] == '.' { // .이면 체크할 필요 없음 | |
continue | |
} | |
n := board[i][j] - '1' //5라는 값은 실제로, 0번째부터 8번까지 있으므로, -1을 해준다. | |
if col[j][n] { //이미 지나간 적이 있는 값이므로, 리턴 | |
return false | |
} | |
if row[i][n] { //이미 지나간 적이 있는 값이므로, 리턴 | |
return false | |
} | |
b := i/3*3 + j/3 //이 식을 통해서, for loop가 돌더라도, 현재 어느 작은 네모 인덱스에 속한것인지 알 수 있다. | |
if blk[b][n] { | |
return false | |
} | |
col[j][n] = true //지나감을 체크 | |
row[i][n] = true //지나감을 체크 | |
blk[b][n] = true //지나감을 체크 | |
} | |
} | |
return true | |
} | |
func make2DArray() [][]bool { | |
tempArr := make([][]bool, 9) | |
for i := range tempArr { | |
tempArr[i] = make([]bool, 9) | |
} | |
return tempArr | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment