leetcode: Rotate Image

来源:互联网 发布:matlab 编程实践 编辑:程序博客网 时间:2024/05/29 19:52

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?


in-place,先按主对角线呼唤,然后按中轴线互换

class Solution {public:    void rotate(vector<vector<int> > &matrix) {        const int N = matrix.size();        for( int i = 0; i < N; ++i){            for( int j = i + 1; j < N; ++j){//注意j的条件,如果是j=0开始,换了以后又会换回来                    swap( matrix[i][j], matrix[j][i]);            }        }        for( int i = 0, j = N - 1; i < j; ++i, --j){            for( int k = 0; k < N; ++k){                swap( matrix[k][i], matrix[k][j]);            }        }    }};


0 0
原创粉丝点击