LeetCode刷题记录——day10

1、https://leetcode.cn/problems/rotate-image/description/?envType=study-plan-v2&envId=2024-spring-sprint-100

class Solution {
public:
    void rotate(vector<vector<int>>& matrix) {
        int n = matrix.size();
        for (int i = 0; i < n / 2; ++i) {
            for (int j = 0; j < (n + 1) / 2; ++j) {
                int temp = matrix[i][j];
                matrix[i][j] = matrix[n - j - 1][i];
                matrix[n - j - 1][i] = matrix[n - i - 1][n - j - 1];
                matrix[n - i - 1][n - j - 1] = matrix[j][n - i - 1];
                matrix[j][n - i - 1] = temp;
            }
        }
    }
};

2、https://leetcode.cn/problems/set-matrix-zeroes/?envType=study-plan-v2&envId=2024-spring-sprint-100

class Solution {
public:
    void setZeroes(vector<vector<int>>& matrix) {
        int m=matrix.size(),n=matrix[0].size();
        int temp[m][n];
        memset(temp,-1,sizeof(temp));
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                if(matrix[i][j]==0){
                    temp[i][j]=1;
                }
            }
        }
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                if(temp[i][j]==1){
                    for(int k=0;k<m;k++)
                        matrix[k][j]=0;
                    for(int k=0;k<n;k++)
                        matrix[i][k]=0;
                }
            }
        }
    }
};
posted @ 2024-04-08 09:15  想成为编程高手的阿曼  阅读(6)  评论(0编辑  收藏  举报