zoukankan      html  css  js  c++  java
  • leetcode@ [48] Rotate Image

    https://leetcode.com/problems/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?

    class Solution {
    public:
        void rotate(vector<vector<int>>& matrix) {
            int n = matrix.size();
            if(n == 0) return;
            
            for(int layer = 0; layer < n/2; ++layer) {
                int first = layer, last = n-1-layer;
                
                for(int i = first; i < last; ++i) {
                    int offset = i - first;
                    
                    //save the top.
                    int top = matrix[first][i];
                    
                    //left -> top.
                    matrix[first][i] = matrix[last-offset][first];
                    
                    //buttom -> left.
                    matrix[last-offset][first] = matrix[last][last-offset];
                    
                    //right -> buttom.
                    matrix[last][last-offset] = matrix[i][last];
                    
                    //top -> right.
                    matrix[i][last] = top;
                }
            }
        }
    };
    View Code
  • 相关阅读:
    IDEA
    elasticsearch java api
    Java-HttpUtil
    spring集成mybatis-plus
    linux语句速查
    linux切换jdk
    《Java并发编程实战》读书笔记
    设计模式六大原则
    Linux 启动管理
    Linux 日志管理
  • 原文地址:https://www.cnblogs.com/fu11211129/p/4968964.html
Copyright © 2011-2022 走看看