Leetcode: Rotate Image

来源:互联网 发布:php模拟post提交数据 编辑:程序博客网 时间:2024/05/02 15:32

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?

void rotate(vector<vector<int> > &matrix) {        // Note: The Solution object is instantiated only once.int n = matrix.size();        if(n<2)return;int loop=0;while(loop<(n+1)/2){int tmp = 0;for(int i = loop; i< n-1-loop;i++){tmp = matrix[loop][i];matrix[loop][i] = matrix[n-1-i][loop];matrix[n-1-i][loop] = matrix[n-1-loop][n-1-i];matrix[n-1-loop][n-1-i] = matrix[i][n-1-loop];matrix[i][n-1-loop] = tmp;}loop++;}    }





原创粉丝点击