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

力扣题-10.5

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

力扣题1:566. 重塑矩阵

解题思想:遍历数据即可

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

class Solution(object):
    def matrixReshape(self, mat, r, c):
        """
        :type mat: List[List[int]]
        :type r: int
        :type c: int
        :rtype: List[List[int]]
        """
        m = len(mat)
        n = len(mat[0])
        matrix = [[0 for _ in range(c)] for _ in range(r)]
        if m*n!=r*c:
            return mat
        else:
            tempi = 0
            tempj = 0
            for i in range(m):
                for j in range(n):
                    num = mat[i][j]
                    if tempj<c:
                        matrix[tempi][tempj] = num
                        tempj = tempj+1
                    else:
                        tempj =0
                        tempi =tempi+1
                        matrix[tempi][tempj] = num
                        tempj = tempj+1
            return matrix
class Solution {
public:
    vector<vector<int>> matrixReshape(vector<vector<int>>& mat, int r, int c) {
        int m = mat.size();
        int n = mat[0].size();
        std::vector<std::vector<int>> matrix(r, std::vector<int>(c, 0));

        if (m * n != r * c) {
            return mat;
        } else {
            int tempi = 0;
            int tempj = 0;
            for (int i = 0; i < m; ++i) {
                for (int j = 0; j < n; ++j) {
                    int num = mat[i][j];
                    if (tempj < c) {
                        matrix[tempi][tempj] = num;
                        tempj = tempj + 1;
                    } else {
                        tempj = 0;
                        tempi = tempi + 1;
                        matrix[tempi][tempj] = num;
                        tempj = tempj + 1;
                    }
                }
            }
            return matrix;
        }
    }
};

力扣题2:48. 旋转图像

解题思想:因为只能原地旋转,先对矩阵进行水平翻转,再对矩阵进行对角线翻转即可

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

class Solution(object):
    def rotate(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: None Do not return anything, modify matrix in-place instead.
        """
        m = len(matrix)
        ##进行水平翻转
        for i in range(int(m/2)):
            for j in range(m):
                temp = matrix[i][j]
                matrix[i][j] = matrix[m-1-i][j]
                matrix[m-1-i][j] = temp
        ##进行主对角线翻转,遍历下三角
        for i in range(m):
            for j in range(i):
                temp = matrix[i][j]
                matrix[i][j] = matrix[j][i]
                matrix[j][i] = temp
class Solution {
public:
    void rotate(vector<vector<int>>& matrix) {
        int m = matrix.size();
        int temp=0;
        for(int i=0;i<int(m/2);i++){
            for(int j=0;j<m;j++){
                temp = matrix[i][j];
                matrix[i][j] = matrix[m-1-i][j];
                matrix[m-1-i][j] = temp;
            }
        }
        for(int i=0;i<m;i++){
            for(int j=0;j<i;j++){
                temp = matrix[i][j];
                matrix[i][j] = matrix[j][i];
                matrix[j][i] = temp;
            }
        }
    }
};

你可能感兴趣的:(leetcode)