旋转矩阵
给你一幅由 N × N 矩阵表示的图像,其中每个像素的大小为 4 字节。请你设计一种算法,将图像旋转 90 度。
不占用额外内存空间能否做到?
示例 1:
给定 matrix =
[
[1,2,3],
[4,5,6],
[7,8,9]
],
原地旋转输入矩阵,使其变为:
[
[7,4,1],
[8,5,2],
[9,6,3]
]
示例 2:
给定 matrix =
[
[ 5, 1, 9,11],
[ 2, 4, 8,10],
[13, 3, 6, 7],
[15,14,12,16]
],
原地旋转输入矩阵,使其变为:
[
[15,13, 2, 5],
[14, 3, 4, 1],
[12, 6, 8, 9],
[16, 7,10,11]
]
注意:本题与主站 48 题相同:https://leetcode-cn.com/problems/rotate-image/
2种方法(VS2013):
#include <iostream>
#include <string>
#include <cstdio>
#include <vector>
using namespace std;
void printMatrix(int matrix[][4])
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
void printMatrix(vector<vector<int>>& vecTest)
{
auto iter = vecTest.begin();
for (; iter != vecTest.end(); ++iter)
{
for (int i = 0; i< (*iter).size(); ++i)
{
cout << (*iter)[i] << " ";
}
cout << endl;
}
cout << endl;
}
void coordinate(int matrix[][4], int leftStartCoordinate[], int rightEndCoordinate[])
{
int left_up_x = leftStartCoordinate[0];//0
int left_up_y = leftStartCoordinate[1];//0
int left_down_x = rightEndCoordinate[0];//3
int left_down_y = leftStartCoordinate[1];//0
int right_up_x = leftStartCoordinate[0];//0
int right_up_y = rightEndCoordinate[1];//3
int right_down_x = rightEndCoordinate[0];//3
int right_down_y = rightEndCoordinate[1];//3
cout << " " << endl;
cout << left_up_x << left_up_y << endl;
cout << left_down_x << left_down_y << endl;
cout << right_up_x << right_up_y << endl;
cout << right_down_x << right_down_y << endl;
cout << " " << endl;
/*
left_up_x left_up_y right_up_x right_up_y
left_down_x left_down_y right_down_x right_down_y
*/
while (left_up_y < right_up_y)
{
int tmp = matrix[left_up_x][left_up_y];
matrix[left_up_x][left_up_y] = matrix[left_down_x][left_down_y];
matrix[left_down_x][left_down_y] = matrix[right_down_x][right_down_y];
matrix[right_down_x][right_down_y] = matrix[right_up_x][right_up_y];
matrix[right_up_x][right_up_y] = tmp;
left_up_y++;
right_up_x++;
right_down_y--;
left_down_x--;
}
}
///方法一:矩阵旋转
void matrixRotate(int matrix[][4])
{
int leftStartCoordinate[] = {0, 0};
int rightEndCoordinate[] = {3, 3};
while (leftStartCoordinate[0] < rightEndCoordinate[0] && leftStartCoordinate[1] < rightEndCoordinate[1])
{
coordinate(matrix, leftStartCoordinate, rightEndCoordinate);
printMatrix(matrix);
leftStartCoordinate[0]++;
leftStartCoordinate[1]++;
rightEndCoordinate[0]--;
rightEndCoordinate[1]--;
}
}
///方法2:矩阵旋转 (右转90°)
class Solution {
public:
void rotate(vector<vector<int>>& matrix)
{
cout << "--1" << endl;
//1. 对角翻转
for (auto row = 0; row<matrix.size(); row++)
{
cout << endl;
for (auto column = 0; column <= row; column++)
{
cout << "(" << row << column << "); " << "(" << column << row << ") " << endl;
swap(matrix[row][column], matrix[column][row]);
}
}
cout << endl;
printMatrix(matrix);
cout << "--2" <<endl;
cout << endl;
// 2. 镜像翻转
for (int row = 0; row<matrix.size(); row++)
{
cout << endl;
for (int column = 0; column<matrix.size() / 2; column++)
{
cout << "(" << row << column << "); " << "(" << row << matrix.size() - column - 1 << ") " << endl;
swap(matrix[row][column], matrix[row][matrix.size() - column - 1]);
}
}
cout << endl;
printMatrix(matrix);
}
};
int main(int argc, char *argv[])
{
cout << "------method 1:" << endl;
int matrix[4][4] = {{ 1, 2, 3, 4 }, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}};
printMatrix(matrix);
matrixRotate(matrix);//方法1
printMatrix(matrix);
cout << "------method2:" << endl;
vector<int> vec1;
vec1.push_back(1);
vec1.push_back(2);
vec1.push_back(3);
vec1.push_back(4);
vector<int> vec2;
vec2.push_back(5);
vec2.push_back(6);
vec2.push_back(7);
vec2.push_back(8);
vector<int> vec3;
vec3.push_back(9);
vec3.push_back(10);
vec3.push_back(11);
vec3.push_back(12);
vector<int> vec4;
vec4.push_back(13);
vec4.push_back(14);
vec4.push_back(15);
vec4.push_back(16);
vector<vector<int>> matrix_vector;
matrix_vector.push_back(vec1);
matrix_vector.push_back(vec2);
matrix_vector.push_back(vec3);
matrix_vector.push_back(vec4);
//方法2
Solution solution;
printMatrix(matrix_vector);
solution.rotate(matrix_vector);
printMatrix(matrix_vector);
system("pause");
return EXIT_SUCCESS;
}
leet官方答案: