• [LeetCode] 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?

    思路一:

    b[i][j] = a[n-1-j][i], 构造b,然后将b赋值给a,空间复杂度O(n*n)

    class Solution {
        public:
            void rotate(vector<vector<int> > &matrix)
            {   
                size_t n = matrix.size();
                vector<vector<int> > re; 
                vector<int> b;
                b.resize(n);
    
                for(int i = 0; i < n; i ++) 
                    re.push_back(b);
    
    
                for(int i = 0; i < n; i ++) 
                    for(int j = 0; j < n; j ++) 
                    {   
                      re[i][j] = matrix[n-j-1][i]; 
                    }   
    
                for(int i = 0; i < n; i ++) 
                    for(int j = 0; j < n; j ++) 
                      matrix[i][j] = re[i][j]; 
            }   
    };

    思路二:旋转90°= 第一步, 沿着副对角线翻转+ 第二步,沿着水平中轴线翻转,空间复杂度O(1)

    class Solution {
        public:
        #if 0
            void rotate(vector<vector<int> > &matrix)
            {   
                size_t n = matrix.size();
                vector<vector<int> > re; 
                vector<int> b;
                b.resize(n);
    
                for(int i = 0; i < n; i ++) 
                    re.push_back(b);
    
    
                for(int i = 0; i < n; i ++) 
                    for(int j = 0; j < n; j ++) 
                    {   
                      re[i][j] = matrix[n-j-1][i]; 
                    }   
    
                for(int i = 0; i < n; i ++) 
                    for(int j = 0; j < n; j ++) 
                      matrix[i][j] = re[i][j]; 
            }  
        #endif
                public:
            void rotateFromDiagonal (vector<vector<int> > &matrix)
            {
                size_t n = matrix.size();
                int tmp;
                for(int i = 0; i < n; i ++)
                    for(int j = 0; j <  n-i; j ++)
                    {
                        tmp = matrix[i][j];
                        matrix[i][j] = matrix[n-j-1][n-i-1];
                        matrix[n-j-1][n-i-1] = tmp;
                    }
            }
    
            void rotateFromHorizon(vector<vector<int> > &matrix)
            {
                size_t n = matrix.size();
                int tmp;
                for(int i = 0; i < n/2; i ++)
                    for(int j = 0; j < n; j ++)
                    {
                        tmp = matrix[i][j];
                        matrix[i][j] = matrix[n-1-i][j];
                        matrix[n-1-i][j] = tmp;
                    }
            }
    
            void rotate(vector<vector<int> > &matrix)
            {
                rotateFromDiagonal(matrix);
                rotateFromHorizon(matrix);
            }
    };
  • 相关阅读:
    [转]Angular2-组件间数据传递的两种方式
    [转]Angular4---部署---将Angular项目部署到IIS上
    [转]Angular开发(十八)-路由的基本认识
    [转]【Angular4】基础(一):脚手架 Angular CLI
    [转]【Angular4】基础(二):创建组件 Component
    [转]Angular项目目录结构详解
    [转]Ionic国际化解决方案
    [转]Angular CLI 安装和使用
    [转]nodejs之cordova 跨平台开发
    [转]Windows下配置Node.js和Cordova
  • 原文地址:https://www.cnblogs.com/diegodu/p/4310811.html
Copyright © 2020-2023  润新知