1. 程式人生 > >48. Rotate Image

48. Rotate Image

題目:

1541564745525

解答:

外圈向內圈逐步旋轉

程式碼:

class Solution {
public:
    void rotate(vector<vector<int> > &matrix) {
        int len = matrix.size();
        if( len == 1 || len == 0 )  return;
        for( int i = 0;2 * i + 1 < len;++i )    {
            int row = i, col = i, maxcol = len - i - 1;
            for
(;col < maxcol;++col ) { int temp = matrix[row][col]; matrix[row][col] = matrix[len - col - 1][row]; matrix[len - col - 1][row] = matrix[len - row - 1][len - col - 1]; matrix[len - row - 1][len - col - 1] = matrix[col][len - row - 1
]; matrix[col][len - row - 1] = temp; } } } };