zoukankan      html  css  js  c++  java
  • [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?

    问题描述:给定一个n*n的二维矩阵,它代表的是一幅图片,将这个图片,也就是矩阵顺时针旋转90度。

    我采用的方法和之前Spiral Matrix II类似,一圈一圈地向里面推进。然后用一个变量来作为中转。

    class Solution {
    public:
        void rotate(vector<vector<int> > &matrix) {
            // IMPORTANT: Please reset any member data you declared, as
            // the same Solution instance will be reused for each test case.
            int n = matrix.size();
    
            int temp = 0;
            int i = 0, j = 0;
            int start = 0, end = n-1;
            int index = 0;
            int width = 0;
            width = n-1;
            while(start < end) {
                for(j = start; j < end; j++) {
                    temp = matrix[index][j];
                    matrix[index][j] = matrix[width-j][index];
                    matrix[width-j][index] = matrix[width-index][width-j];
                    matrix[width-index][width-j] = matrix[j][width-index];
                    matrix[j][width-index] = temp;
                }
                index++;
                start++;
                end--;
            }
        }
    };


  • 相关阅读:
    golang单例模式
    PHP打开并修改文件
    关于文件服设计的一些想法
    Api
    golang Iterate through the fields of a struct in Go
    zookeeper note
    centos 6 install protoc
    todo
    linux route
    build http_load
  • 原文地址:https://www.cnblogs.com/keanuyaoo/p/3395462.html
Copyright © 2011-2022 走看看