|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
题目描述:
- 给定一个 n × n 的二维矩阵表示一个图像。
- 将图像顺时针旋转 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]
- ]
- 来源:力扣(LeetCode)
- 链接:https://leetcode-cn.com/problems/rotate-image
- 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
复制代码
- class Solution {
- public:
- void rotate(vector<vector<int>>& matrix) {
- int up = 0, down = matrix.size() - 1;
- int left = 0, right = matrix[0].size() - 1;
- while(up < down && left < right){
- int temp = 0;
- while(temp != right - left){
- swap(matrix[up][left + temp], matrix[down - temp][left]);
- swap(matrix[down - temp][left], matrix[up + temp][right]);
- swap(matrix[down - temp][left], matrix[down][right - temp]);
- temp ++;
- }
- up++;
- down--;
- left++;
- right--;
- }
- return;
- }
- };
复制代码
参考链接:https://leetcode-cn.com/problems ... ng-de-fang-fa-zhu-/ |
|