Leetcode数据结构题目集第五天,有效的数独矩阵置零.
其中矩阵置零比较有趣放在前面,有效的数独只是代码量比较长,逻辑部分并不难。

矩阵置零

给定一个 m x n 的矩阵,如果一个元素为 0 ,则将其所在行和列的所有元素都设为 0 。请使用原地算法。

进阶:

  • 一个直观的解决方案是使用 O(mn) 的额外空间,但这并不是一个好的解决方案。
  • 一个简单的改进方案是使用 O(m + n) 的额外空间,但这仍然不是最好的解决方案。
    你能想出一个仅使用常量空间的解决方案吗?

示例 1:

1
2
输入:matrix = [[1,1,1],[1,0,1],[1,1,1]]
输出:[[1,0,1],[0,0,0],[1,0,1]]

示例 2:

1
2
输入:matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]
输出:[[0,0,0,0],[0,4,5,0],[0,3,1,0]]

提示:

  • m == matrix.length
  • n == matrix[0].length
  • 1 <= m, n <= 200
  • -231 <= matrix[i][j] <= 231 - 1

解题思路&题解

通过观察发现置零是将0的行号和列号记录下来,然后将记录下来的行列置零即可
具体思路如下:

  • 当同一行有一个0时,则该行后面遇到0值时也无需关系这一行的变化->因为一行里面有一个0,整行都是0(列同理)
  • 在同一行有两个0时需要注意,这意味着有两列也需要置零。

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
func setZeroes(matrix [][]int) {
row, col := []int{}, []int{}
for r := 0; r < len(matrix); r++ {
for c := 0; c < len(matrix[r]); c++ {
if matrix[r][c] == 0 {
// 记录
row = append(row, r)
col = append(col, c)
}
}
}

for _, item := range row {
for index := range matrix[item] {
matrix[item][index] = 0
}
}

for _, item := range col {
for _, value := range matrix {
value[item] = 0
}
}
}

有效的数独

请你判断一个 9x9 的数独是否有效。只需要 根据以下规则 ,验证已经填入的数字是否有效即可。
数字 1-9 在每一行只能出现一次。
数字 1-9 在每一列只能出现一次。
数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。(请参考示例图)
数独部分空格内已填入了数字,空白格用 ‘.’ 表示。

注意:

  • 一个有效的数独(部分已被填充)不一定是可解的。
  • 只需要根据以上规则,验证已经填入的数字是否有效即可。

示例 1:

1
2
3
4
5
6
7
8
9
10
11
输入:board = 
[["5","3",".",".","7",".",".",".","."]
,["6",".",".","1","9","5",".",".","."]
,[".","9","8",".",".",".",".","6","."]
,["8",".",".",".","6",".",".",".","3"]
,["4",".",".","8",".","3",".",".","1"]
,["7",".",".",".","2",".",".",".","6"]
,[".","6",".",".",".",".","2","8","."]
,[".",".",".","4","1","9",".",".","5"]
,[".",".",".",".","8",".",".","7","9"]]
输出:true

示例 2:

1
2
3
4
5
6
7
8
9
10
11
12
输入:board = 
[["8","3",".",".","7",".",".",".","."]
,["6",".",".","1","9","5",".",".","."]
,[".","9","8",".",".",".",".","6","."]
,["8",".",".",".","6",".",".",".","3"]
,["4",".",".","8",".","3",".",".","1"]
,["7",".",".",".","2",".",".",".","6"]
,[".","6",".",".",".",".","2","8","."]
,[".",".",".","4","1","9",".",".","5"]
,[".",".",".",".","8",".",".","7","9"]]
输出:false
解释:除了第一行的第一个数字从 5 改为 8 以外,空格内其他数字均与 示例1 相同。 但由于位于左上角的 3x3 宫内有两个 8 存在, 因此这个数独是无效的。

提示:

  • board.length == 9
  • board[i].length == 9
  • board[i][j] 是一位数字或者 ‘.’

解题思路&题解

题目没什么难度,唯一需要注意的是:判断子矩阵的时候,是根据当前坐标/矩阵大小来决定的。
代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
func isValidSudoku(board [][]byte) bool {
rowBuf := make([][9]bool, 9)
colBuf := make([][9]bool, 9)
boxBuf := make([][9]bool, 9)

for row:=0; row<9; row++ {
for col:=0; col<9; col++ {
if board[row][col] == '.' {
continue
}

val := board[row][col] - '1'
if rowBuf[row][val] || colBuf[col][val] || boxBuf[row/3*3+col/3][val] {
return false
}

rowBuf[row][val] = true
colBuf[col][val] = true
boxBuf[row/3*3+col/3][val] = true
}
}
return true
}

func validDigit(digit byte) bool {
return digit >= '1' && digit <= '9'
}

func inRow(rows []byte, col int, digit byte) bool {
for c := 0; c < 9; c++ {
if rows[c] == digit && c != col {
return true
}
}
return false
}

func inColumn(g [][]byte, column, row int, digit byte) bool {
for r := 0; r < 9; r++ {
if g[r][column] == digit && r != row {
return true
}
}
return false
}

func inRegion(g [][]byte, row, column int, digit byte) bool {
startRow, startColumn := row/3*3, column/3*3
for r := startRow; r < startRow+3; r++ {
for c := startColumn; c < startColumn+3; c++ {
if g[r][c] == digit && r != row && c != column {
return true
}
}
}
return false
}