• Rotate Image——数学相关


    You are given an n x n 2D matrix representing an image.

    Rotate the image by 90 degrees (clockwise).

    Follow up:
    Could you do this in-place?

    这题有三种思路:

    思路一:

    找到移动前后移动后位置的关系,直接交换位置,但是这样做需要额外的空间。

    思路二:

    每次移动一位,直到旋转90度。

    思路三:

    转:http://www.2cto.com/kf/201401/274473.html

    使用3的代码应该是最简洁的。

    class Solution {
    public:
        void rotate(vector<vector<int>>& matrix) {
            int i,j,temp;
            int n=matrix.size();
            // 沿着副对角线反转
            for (int i = 0; i < n; ++i) {
                for (int j = 0; j < n - i; ++j) {
                    temp = matrix[i][j];
                    matrix[i][j] = matrix[n - 1 - j][n - 1 - i];
                    matrix[n - 1 - j][n - 1 - i] = temp;
                }
            }
            // 沿着水平中线反转
            for (int i = 0; i < n / 2; ++i){
                for (int j = 0; j < n; ++j) {
                    temp = matrix[i][j];
                    matrix[i][j] = matrix[n - 1 - i][j];
                    matrix[n - 1 - i][j] = temp;
                }
            }
            
        }
    };
  • 相关阅读:
    软件工程第二次作业
    软件工程第一次作业
    5T-时延小结
    4T--5G网络时延
    2T--网络切片
    1T--5G 三大应用场景
    2020软件工程第一次作业
    软件工程最后一次作业
    软件工程第四次作业
    软件工程第三次作业
  • 原文地址:https://www.cnblogs.com/qiaozhoulin/p/4579906.html
Copyright © 2020-2023  润新知