[LeetCode] Rotate Image

来源:互联网 发布:java io开销 编辑:程序博客网 时间:2024/06/05 02:37
void rotate(vector<vector<int> > &matrix) {int n = matrix.size();for(int row = 0; row < n/2; row++){for(int col = 0; col < (n+1)/2; col++){int ele = matrix[row][col];int cnt = 0;while(cnt < 4){int newRow = col, newCol = n-row-1;int tmp = matrix[newRow][newCol];matrix[newRow][newCol] = ele;ele = tmp;row = newRow;col = newCol;cnt++;}}}}


(row, col) -> (col, n-row-1) -> (n-row-1, n-col-1) -> (n-col-1, row) -> (row, col)

如上,四个位置的像素值依次传递,这四个像素从而完成了右旋90度;

图片左上半区的像素都如此做传递,则整张图片完成右旋90度。

0 0
原创粉丝点击