讨论社区

为什么37题解数组,同一份测试,但是执行代码和提交结果不一样

同一份代码(代码在最后),同一个测试样例,执行代码通过,提交会出错??


1.测试样例:

image.png


2.然后我把输入拷贝到测试区


image.png


3.点击执行代码,就可以通过(注意,我这里点了差别,可以看到是和答案没有差别的)


image.png



代码:


var row []map[byte]bool

var col []map[byte]bool

var box []map[byte]bool

var boardCopy [][]byte

var solveFlag bool


func solveSudoku(board [][]byte) {

if len(board) == 0 {

return

}

initGrid(board)

backTrace(0, 0)


}


func placeNextNumver(i, j int) {

if i == 8 && j == 8 {

    solveFlag = true} 

else {solveFlag = true

if j == 8 {

backTrace(i+1, 0)

} else {

backTrace(i, j+1)

}

}

}


func backTrace(i, j int) {

if boardCopy[i][j] == '.' {

for number := 1; number < 10; number++ {

b := intToByte(number)

idx := (i/3)*3 + j/3

if row[i][b] || col[j][b] || box[idx][b] {

continue

} else {

placeNumber(i, j, number)

placeNextNumver(i, j)

if solveFlag {

return

} else {

removeNumber(i, j, number)

}

}

}

} else {

placeNextNumver(i, j)

}

}


func placeNumber(i, j int, number int) {

b := intToByte(number)

boardCopy[i][j] = b

idx := (i/3)*3 + j/3

row[i][b] = true

col[j][b] = true

box[idx][b] = true

}


func removeNumber(i, j int, number int) {

b := intToByte(number)

boardCopy[i][j] = '.'

idx := (i/3)*3 + j/3

row[i][b] = false

col[j][b] = false

box[idx][b] = false

}


func intToByte(i int) byte {

str := strconv.FormatInt(int64(i), 10)

return []byte(str)[0]

}


func initGrid(board [][]byte) {

row = make([]map[byte]bool, 9)

col = make([]map[byte]bool, 9)

box = make([]map[byte]bool, 9)

for i := 0; i < 9; i++ {

row[i] = make(map[byte]bool)

col[i] = make(map[byte]bool)

box[i] = make(map[byte]bool)

}

boardCopy = board


for i := 0; i < 9; i++ {

for j := 0; j < 9; j++ {

if number := board[i][j]; number != '.' {

idx := (i/3)*3 + j/3

row[i][number] = true

col[j][number] = true

box[idx][number] = true

}

}

}


}


测试案例:    


[[".",".","9","7","4","8",".",".","."],["7",".",".",".",".",".",".",".","."],[".","2",".","1",".","9",".",".","."],[".",".","7",".",".",".","2","4","."],[".","6","4",".","1",".","5","9","."],[".","9","8",".",".",".","3",".","."],[".",".",".","8",".","3",".","2","."],[".",".",".",".",".",".",".",".","6"],[".",".",".","2","7","5","9",".","."]]



0 人关注了该问题 关注

0

feiceh • 2月前

推荐阅读:「为什么某些测试用例下,执行代码返回结果正确,但提交解答却出错了」。后续刷题学习中遇到困难,可通过阅读「题解」解决做题中的一些困惑,也可至 力扣圈子 进行题目交流,以获得更好的支持哟~

0 个讨论

您需要登录后才可回复
您需要登录后才可以回复