48. Rotate Image
阿新 • • 發佈:2018-12-21
題目:
解答:
外圈向內圈逐步旋轉
程式碼:
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;
}
}
}
};