给定一个 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] ]

顺时针旋转矩阵的步骤

1.把矩阵上下翻转。比如[1,2][3,4] -> [3, 4][1, 2]

2.将矩阵沿着从左上角到右下角的对角线进行翻转

如果是逆时针

则将1,2步倒着操作

如果记不住顺序,可以试着用二维数组模拟一下

class Solution {
public:
void rotate(vector<vector<int> >& matrix)
{
int r = matrix.size();
if(r == 0)
return;
int c = matrix[0].size();
for(int i = 0; i < r / 2; i++)
{
for(int j = 0; j < c; j++)
{
swap(matrix[i][j], matrix[r - 1 - i][j]);
}
}
for(int i = 0; i < r; i++)
{
for(int j = i + 1; j < c; j++)
{
swap(matrix[i][j], matrix[j][i]);
}
}
}
};

Leetcode48. Rotate Image旋转图像的更多相关文章

  1. 048 Rotate Image 旋转图像

    给定一个 n × n 的二维矩阵表示一个图像.将图像旋转 90 度(顺时针).注意:你必须在原矩阵中旋转图像,请不要使用另一个矩阵来旋转图像.例 1:给出的输入矩阵 = [  [1,2,3],  [4 ...

  2. [LeetCode] Rotate Image 旋转图像

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  3. LeetCode48 Rotate Image

    题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...

  4. leetCode 48.Rotate Image (旋转图像) 解题思路和方法

    Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ...

  5. [leetcode]48. Rotate Image旋转图像

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  6. Rotate Image 旋转图像

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  7. [LeetCode] 48. Rotate Image 旋转图像

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  8. [CareerCup] 1.6 Rotate Image 翻转图像

    1.6 Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a m ...

  9. [LeetCode] 61. Rotate List 旋转链表

    Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...

随机推荐

  1. 纯CSS样式写刘海屏效果

    1. 效果: 2. 代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...

  2. [洛谷P1966] 火柴排队

    题目链接: 火柴排队 题目分析: 感觉比较顺理成章地就能推出来?似乎是个一眼题 交换的话多半会往逆序对上面想,然后题目给那个式子就是拿来吓人的根本没有卵用 唯一的用处大概是告诉你考虑贪心一波,很显然有 ...

  3. JavaScript性能优化篇js优化

    JavaScript性能优化篇js优化   随着Ajax越来越普遍,Ajax引用的规模越来越大,Javascript代码的性能越来越显得重要,我想这就是一个很典型的例子,上面那段代码因为会被频繁使用, ...

  4. [原创]mysql 5.6安装配置,主从分离,读写分离简单教程

    文章中参考使用了多个博客的资料,汇总而成!其流程准确性被人亦本人实践! https://blog.csdn.net/qq_35206261/article/details/81321201 https ...

  5. <每日一题>题目22:简单的python练习题(31-40)

    #31.分布式爬虫主要解决什么问题? ''' ip 带宽 CPU IO ''' #32.网络传输层 ''' 应用层—http ftp dns nfs 传输层—tcp --udp 网络层—ip icmp ...

  6. C#获取MP3,WMA信息

    用于获取MP3内部信息,包括歌曲名,歌手名等…… namespace FileBatchRemaer.domain { /// <summary> /// Mp3信息结构 /// < ...

  7. css3 做border = 0.5px的细线

    参考: https://blog.csdn.net/Tyro_java/article/details/52013531

  8. Cmp- Linux必学的60个命令

    1.作用 cmp(“compare”的缩写)命令用来简要指出两个文件是否存在差异,它的使用权限是所有用户. 2.格式 cmp[options] 文件名 3.[options]主要参数 -l: 将字节以 ...

  9. 乐观、悲观锁、redis分布式锁

    悲观锁总是假设最坏的情况,每次去拿数据的时候都认为别人会修改,所以每次在拿数据的时候都会上锁,这样别人想拿这个数据就会阻塞直到它拿到锁(共享资源每次只给一个线程使用,其它线程阻塞,用完后再把资源转让给 ...

  10. dump与load

    dump与load 简化了dumps与loads