力扣题:二维数组变换-10.6

力扣题-10.6

[力扣刷题攻略] Re:从零开始的力扣刷题生活

力扣题1:73. 矩阵置零

解题思想:遍历矩阵,记录下需要修改为0的行和列即可

力扣题:二维数组变换-10.6_第1张图片

class Solution(object):
    def setZeroes(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: None Do not return anything, modify matrix in-place instead.
        """
        m = len(matrix)
        n = len(matrix[0])
        flag1 = [0 for _ in range(m)]
        flag2 = [0 for _ in range(n)]
        for i in range(m):
            for j in range(n):
                if matrix[i][j]==0:
                    flag1[i] =1
                    flag2[j] =1
        for i in range(m):
            if flag1[i]==1:
                for j in range(n):
                    matrix[i][j]=0
        for i in range(n):
            if flag2[i]==1:
                for j in range(m):
                    matrix[j][i]=0
class Solution {
public:
    void setZeroes(vector<vector<int>>& matrix) {
        int m = matrix.size();
        int n = matrix[0].size();
        std::vector<int> flag1(m, 0);
        std::vector<int> flag2(n, 0);

        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                if (matrix[i][j] == 0) {
                    flag1[i] = 1;
                    flag2[j] = 1;
                }
            }
        }

        for (int i = 0; i < m; ++i) {
            if (flag1[i] == 1) {
                for (int j = 0; j < n; ++j) {
                    matrix[i][j] = 0;
                }
            }
        }

        for (int i = 0; i < n; ++i) {
            if (flag2[i] == 1) {
                for (int j = 0; j < m; ++j) {
                    matrix[j][i] = 0;
                }
            }
        }
    }
};

力扣题2:289. 生命游戏

解题思想:遍历矩阵进行统计即可

力扣题:二维数组变换-10.6_第2张图片

class Solution(object):
    def gameOfLife(self, board):
        """
        :type board: List[List[int]]
        :rtype: None Do not return anything, modify board in-place instead.
        """
        m = len(board)
        n = len(board[0])

        temp = copy.deepcopy(board)

        for i in range(m):
            for j in range(n):
                num =0
                if i-1>=0 and j-1>=0 and temp[i-1][j-1]==1:
                    num +=1
                if i-1>=0 and temp[i-1][j]==1:
                    num +=1
                if i-1>=0 and j+1<n and temp[i-1][j+1]==1:
                    num +=1
                if j-1>=0 and temp[i][j-1]==1:
                    num +=1
                if j+1<n and temp[i][j+1]==1:
                    num +=1
                if i+1<m and j-1>=0 and temp[i+1][j-1]==1:
                    num +=1
                if i+1<m and temp[i+1][j]==1:
                    num +=1
                if i+1<m and j+1<n and temp[i+1][j+1]==1:
                    num +=1
                if board[i][j]==1 and num<2:
                    board[i][j]=0
                elif board[i][j]==1 and num>3:
                    board[i][j]=0
                elif board[i][j]==0 and num==3:
                    board[i][j]=1
class Solution {
public:
    void gameOfLife(vector<vector<int>>& board) {
        int m = board.size();
        int n = board[0].size();
        vector<vector<int>> temp = board;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                int num = 0;
                if (i - 1 >= 0 && j - 1 >= 0 && temp[i - 1][j - 1] == 1) {
                    num++;
                }
                if (i - 1 >= 0 && temp[i - 1][j] == 1) {
                    num++;
                }
                if (i - 1 >= 0 && j + 1 < n && temp[i - 1][j + 1] == 1) {
                    num++;
                }
                if (j - 1 >= 0 && temp[i][j - 1] == 1) {
                    num++;
                }
                if (j + 1 < n && temp[i][j + 1] == 1) {
                    num++;
                }
                if (i + 1 < m && j - 1 >= 0 && temp[i + 1][j - 1] == 1) {
                    num++;
                }
                if (i + 1 < m && temp[i + 1][j] == 1) {
                    num++;
                }
                if (i + 1 < m && j + 1 < n && temp[i + 1][j + 1] == 1) {
                    num++;
                }
                        
                if (board[i][j] == 1 && num < 2) {
                    board[i][j] = 0;
                }
                else if (board[i][j] == 1 && num > 3) {
                    board[i][j] = 0;
                }
                else if (board[i][j] == 0 && num == 3) {
                    board[i][j] = 1;
                }
            }
        }
    }
};

你可能感兴趣的:(leetcode)