---
# System prepended metadata

title: '**Leetcode筆記(Set Matrix Zeroes)**'
tags: [medium, matrix, microsoft, leetcode]

---

# **Leetcode筆記(Set Matrix Zeroes)**


:::info
:information_source: 題目 : Set Matrix Zeroes, 類型 : matrix , 等級 : medium
日期 : 2023/02/28，2023/08/12，2024/05/21，2024/10/09
:::


### 嘗試

時間複雜度O(nm)，空間複雜度O(1)

```python
class Solution:
    def setZeroes(self, matrix: List[List[int]]) -> None:
        row, col = len(matrix), len(matrix[0])
        RowList = []
        ColList = []
        for i in range(row):
            for j in range(col):
                if matrix[i][j] == 0:
                    RowList.append(i)
                    ColList.append(j)
        for j in range(col):
            for i in range(len(RowList)):
                RowNum = RowList[i]      
                matrix[RowNum][j] = 0
        for i in range(row):
            for j in range(len(ColList)):
                ColNum = ColList[j]      
                matrix[i][ColNum] = 0
```
2023/08/12
用兩個變數去紀錄第一行與第一列有無0，看完後，就用第一行與第一列去記錄剩下的區塊，(如果剩下的區塊有0，那就把0往上放與往右放)，後面用這個原理，去更新剩餘區塊，最後再來更新第一行與第一列
```python
class Solution:
    def setZeroes(self, matrix: List[List[int]]) -> None:
        m, n = len(matrix), len(matrix[0])
        FirstRow, FirstCol = False, False

        # 第一行和第一列
        for i in range(m):
            if matrix[i][0] == 0:
                FirstCol = True
        for j in range(n):
            if matrix[0][j] == 0:
                FirstRow = True

        # 剩餘區塊
        for i in range(1, m):
            for j in range(1, n):
                if matrix[i][j] == 0:
                    matrix[i][0] = 0
                    matrix[0][j] = 0
        
        # 剩餘區塊 橫向看
        for i in range(1, m):
            if matrix[i][0] == 0:
                for j in range(1, n):
                    matrix[i][j] = 0
        
        # 剩餘區塊 直向看
        for j in range(1, n):
            if matrix[0][j] == 0:
                for i in range(1, m):
                    matrix[i][j] = 0
        
        # 第一行和第一列
        if FirstCol: 
            for i in range(m):
                matrix[i][0] = 0
        if FirstRow:
            for j in range(n):
                matrix[0][j] = 0
```
2023/12/10
```python
class Solution(object):
    def setZeroes(self, matrix):
        first_row, first_col = False, False
        n, m = len(matrix), len(matrix[0])

        # first col & row
        for i in range(n):
            if matrix[i][0] == 0:
                first_col = True
                break
        
        for j in range(m):
            if matrix[0][j] == 0:
                first_row = True
                break
        
        # rest
        for i in range(1, n):
            for j in range(1, m):
                if matrix[i][j] == 0:
                    matrix[i][0] = 0
                    matrix[0][j] = 0
        
        # set rest
        for i in range(1, n):
            if matrix[i][0] == 0:
                for j in range(1, m):
                    matrix[i][j] = 0
        
        for j in range(1, m):
            if matrix[0][j] == 0:
                for i in range(1, n):
                    matrix[i][j] = 0
        
        # set first
        if first_col:
            for i in range(n):
                matrix[i][0] = 0 
        if first_row:
            for j in range(m):
                matrix[0][j] = 0 

        return matrix    
        
```
2024/05/21
```python
class Solution:
    def setZeroes(self, matrix: List[List[int]]) -> None:
        firstCol, firstRow = False, False
        m, n = len(matrix), len(matrix[0])

        for i in range(m):
            if matrix[i][0] == 0:
                firstCol = True
        
        for j in range(n):
            if matrix[0][j] == 0:
                firstRow = True

        for i in range(1, m):
            for j in range(1, n):
                if matrix[i][j] == 0:
                    matrix[i][0] = 0
                    matrix[0][j] = 0
        
        # start set up
        for i in range(1, m):
            if matrix[i][0] == 0:
                for j in range(1, n):
                    matrix[i][j] = 0

        for j in range(1, n):
            if matrix[0][j] == 0:
                for i in range(1, m):
                    matrix[i][j] = 0
        
        if firstCol:
            for i in range(m):
                matrix[i][0] = 0

        if firstRow:
            for j in range(n):
                matrix[0][j] = 0
```
2024/10/09
```python
class Solution:
    def setZeroes(self, matrix: List[List[int]]) -> None:
        """
        Do not return anything, modify matrix in-place instead.
        """
        firstR, firstC = False, False
        n, m = len(matrix), len(matrix[0])

        for r in range(n):
            if matrix[r][0] == 0:
                firstC = True
                break
        
        for c in range(m):
            if matrix[0][c] == 0:
                firstR = True
                break
        
        # use first row & col as indicator
        for r in range(1, n):
            for c in range(1, m):
                if matrix[r][c] == 0:
                    matrix[r][0] = 0
                    matrix[0][c] = 0
        
        for r in range(1, n):
            if matrix[r][0] == 0:
                for c in range(m):
                    matrix[r][c] = 0
        
        for c in range(1, m):
            if matrix[0][c] == 0:
                for r in range(n):
                    matrix[r][c] = 0
        
        if firstR:
            for c in range(m):
                matrix[0][c] = 0 
        
        if firstC:
            for r in range(n):
                matrix[r][0] = 0
```

---
### **優化**
是用flag的方式
```python
class Solution:
    def setZeroes(self, matrix: List[List[int]]) -> None:
        rows, cols = len(matrix), len(matrix[0])
        # 用到o(1)記憶體 用最後一排當緩衝
        zero_row, zero_col = False, False

        for i in range(rows):
            for j in range(cols):
                if matrix[i][j] == 0:
                    # 第一列
                    if i == 0:
                        zero_row = True
                    # 第一行
                    if j == 0:
                        zero_col = True
                    # 把最上面第一行第一列當成標記
                    matrix[i][0] = matrix[0][j] = 0

        # 剩餘橫列            
        for i in range(1, rows):
            if matrix[i][0] == 0:
                for j in range(1, cols):
                    matrix[i][j] = 0

        # 剩餘直行            
        for j in range(1, cols):
            if matrix[0][j] == 0:
                for i in range(1, rows):
                    matrix[i][j] = 0
        
        # 第一列
        if zero_row:
            for j in range(cols):
                matrix[0][j] = 0
        
        # 第一列
        if zero_col:
            for i in range(rows):
                matrix[i][0] = 0

        return matrix
```
---


**:warning: 錯誤語法** 
:::warning
range()要記得括號
:::


**:thumbsup:學習** 
:::success
若有一矩陣，len(matrix)為row，len(matrix[0])
:::

**思路**


**講解連結**
https://www.youtube.com/watch?v=FPdM2C16Qew&ab_channel=Michelle%E5%B0%8F%E6%A2%A6%E6%83%B3%E5%AE%B6
Provided by. Michelle小梦想家

###### tags: `matrix` `medium` `leetcode`