zoukankan      html  css  js  c++  java
  • 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;
                }
            }
            
        }
    };
  • 相关阅读:
    同步请求和异步请求的区别
    Ajax初步理解
    ajax的GET和POST请求
    What's this?(js)
    rxjs
    Angular7_获取异步方法里面的数据
    Angular7_人员登记系统
    Angular7
    特殊操作符
    Oracle 表操作
  • 原文地址:https://www.cnblogs.com/qiaozhoulin/p/4579906.html
Copyright © 2011-2022 走看看