旋转正方形矩阵
【题目】 给定一个整型正方形矩阵matrix,请把该矩阵调整成 顺时针旋转90度的样子。 【要求】 额外空间复杂度为O(1)。
思路:和绕圈打印矩阵的思路一样以左上和右下两个点确定矩阵的边,然后两个点向中间靠拢
void rotate(vector<vector<int>>&obj, int top_row, int top_line, int end_row, int end_line){
int time = end_line - top_line;
int temp = 0;
for (int i = 0; i < time; i++){
/* tmp = m[tR][tC + i];
m[tR][tC + i] = m[dR - i][tC];
m[dR - i][tC] = m[dR][dC - i];
m[dR][dC - i] = m[tR + i][dC];
m[tR + i][dC] = tmp;*/
temp = obj[top_row][top_line+i];
obj[top_row][top_line+i] = obj[top_row+i][end_line];
obj[top_row+i][end_line] = obj[end_row][end_line-i];
obj[end_row][end_line-i] = obj[end_row-i][top_line];
obj[end_row-i][top_line] = temp;
}
}
void rotate(vector<vector<int>>&obj){
int top_row = 0;
int top_line = 0;
int end_row = obj.size() - 1;
int end_line = obj[0].size() - 1;
while (top_row < end_row)//正方形 就不需要 <= 了
rotate(obj, top_row++, top_line++, end_row--, end_line--);
}