【second】Rotate Image

分两步走:

1. 沿着反对角线做交换(i,j)--》(n-1-j,n-1-i)

2. 沿着中间做交换

 

    void rotate(vector<vector<int> > &matrix) {
        // Note: The Solution object is instantiated only once and is reused by each test case.
        if(matrix.empty()||matrix[0].empty())
            return;
        int n = matrix.size();
        
        for(int i=0;i<n-1;i++)
            for(int j=0;j<n-1-i;j++)
                swap(&matrix[i][j],&matrix[n-1-j][n-1-i]);
        for(int i=0;i<n/2;i++)
            for(int j=0;j<n;j++)
                swap(&matrix[i][j],&matrix[n-1-i][j]);
        
    }
    
    void swap(int* a, int* b)
    {
        int tmp = *a;
        *a = *b;
        *b = tmp;
    }

  

posted @ 2013-10-18 20:44  summer_zhou  阅读(132)  评论(0编辑  收藏  举报