给定一个 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. CLOSE_WAIT问题讨论

    1.https://cloud.tencent.com/developer/article/1347610 2.https://blog.huoding.com/2016/01/19/488 3.ht ...

  2. 深入浅出 Java Concurrency (29): 线程池 part 2 Executor 以及Executors[转]

    Java里面线程池的顶级接口是Executor,但是严格意义上讲Executor并不是一个线程池,而只是一个执行线程的工具.真正的线程池接口是ExecutorService. 下面这张图完整描述了线程 ...

  3. java加拼音的工具 pinyinutil

    <!--汉字转拼音--> <dependency> <groupId>com.belerweb</groupId> <artifactId> ...

  4. C# 判断当前请求是GET、还是POST ?

    方法一: HttpContext.Current.Request.RequestType == "POST"   //当前请求为:POST 方法二: if(Request.Serv ...

  5. C语言中常用的字符串处理函数总结

    C语言中字符串处理函数备注 此文仅用于自己研究和记录 字符串处理函数 1. char *gets(char *s); #include<stdio.h> 功能: 从标准输入读入字符,并保存 ...

  6. Java8的HashMap笔记摘要

    问题例子: HashMap 是不是有序的? 不是有序的. 有没有有序的Map实现类呢?  有 TreeMap 和 LinkedHashMap. TreeMap 和 LinkedHashMap 是如何保 ...

  7. anchor-free : CornerNet 和 CenterNet 简要笔记

    CornerNethourglass network -> prediction module = corner pooling -> heatmaps + embedding + off ...

  8. php报错:Notice: iconv(): Wrong charset, conversion from `GBK' to `UTF8' is not allowed

    代码写错了 $raw_data = iconv("GBK", "UTF8", $raw_data); 改成 $raw_data = iconv("GB ...

  9. python基础---内置函数 和 匿名函数 知识点自查填空题

    1.file ---默认是输出到(),如果设置为(),输出到() 2.sep---打印(),默认为() 3.end---每一次打印的结尾,默认为() 4.flush---立即把内容输出到(),不做() ...

  10. linux管道和tee命令

    ps -ef | grep docker 等价于 ps -ef &> >(grep docker) cat a.log | tee b.txt 等价于 cat a.log & ...