@@ -652,6 +652,25 @@ class Solution:
652
652
r = {'':r, '.':r, '..':r[:-1]}.get(s, r + [s])
653
653
return '/' + '/'.join(r)
654
654
```
655
+
656
+ ## [ 73. 矩阵置零 5行] ( https://leetcode-cn.com/problems/set-matrix-zeroes/ )
657
+ ``` python
658
+ class Solution :
659
+ def setZeroes (self , matrix : List[List[int ]]) -> None :
660
+ row = [[0 ] * len (i) if 0 in i else i for i in matrix]
661
+ col = [[0 ] * len (j) if 0 in j else list (j) for j in zip (* matrix)]
662
+ col2row = list (map (list , zip (* col)))
663
+ # 上面一行效果等同:
664
+ # col2row = [list(i) for i in zip(*col)]
665
+ for i in range (len (matrix)):
666
+ matrix[i] = col2row[i] if row[i] != [0 ] * len (matrix[0 ]) else row[i]
667
+ ```
668
+ - 获取每一行 / 列的值,假如有0 就整行 / 整列置为0
669
+ - 重新将列排序列表转换为行排序列表,即原来的` matrix ` 中有0的列全为0,行不变
670
+ - ` zip(*col) ` 返回的是` zip ` 类型,需要转换成list,其中元素类型为元组
671
+ - 所以之后做了两步转换,先将zip()返回的各个元组转换为list,在将整个转换为list
672
+ - 替换matrix各行, 如果一整行为0, 则替换为0,否则为` col2row ` 对应的各行
673
+
655
674
## [ 78. Subsets 2行] ( https://leetcode.com/problems/subsets/ )
656
675
``` python
657
676
class Solution :
0 commit comments