Skip to content

Latest commit

 

History

History
32 lines (30 loc) · 771 Bytes

36.md

File metadata and controls

32 lines (30 loc) · 771 Bytes

valid-sudoku

解法一

哈希表

func isValidSudoku(board [][]byte) bool {
    row := make([]map[byte]uint8,9,9) // 定义记录器
    col := make([]map[byte]uint8,9,9)
    x3  := make([]map[byte]uint8,9,9)

    //记录器初始化
    for i := 0;i < 9;i++ {
        row[i] =  make(map[byte]uint8)
        col[i] =  make(map[byte]uint8)
        x3[i]  =  make(map[byte]uint8)
    }
    for i := 0;i < 9;i++ {
       for  j := 0;j<9; j++ {  
        curl := board[i][j]
           if curl != '.' {
            row[i][curl]++
            col[j][curl]++
            x3[(i/3)*3+j/3][curl]++
            if row[i][curl]>1 || col[j][curl]>1 || x3[(i/3)*3+j/3][curl]>1 {
                return false
            }
           }
        }
    }
    return true
}