方法一(第一时间想到,可以正确输出,但是不满足题意):
vector<vector<int>> rotate(vector<vector<int>>& matrix)
{
vector<int> ch;
vector<vector<int>> res;
for (int i = 0; i < matrix.size(); ++i)
{
ch.clear();
for (int j = matrix.size()-1; j >=0; --j)
{
ch.push_back(matrix[j][i]);
}
res.push_back(ch);
}
return res;
}
方法二(目前最好):
void rotate(vector<vector<int>>& matrix)
{
for (int i = 0; i < matrix.size(); ++i)
{
for (int j = i+1; j < matrix.size(); ++j)
{
swap(matrix[i][j],matrix[j][i]);
}
reverse(matrix[i].begin(),matrix[i].end());
}
}