You are given an n x n 2D matrix representing an image.

Rotate the image by 90 degrees (clockwise).

Follow up:
Could you do this in-place?

方法一:常规思路: 将图片分为 行数/2 层,然后一层层进行旋转,由外到里。在这个过程中,比较关键的是下标的处理。可从n=3时得出一定的规律。每次旋转时,分析替换的元素对的下标和行、列的关系。如 i=0、j=0时 1:[0][0]换为 7:[2][0]。

代码如下:

 class Solution {
public:
void rotate(vector<vector<int> > &matrix)
{
int n=matrix.size();
for(int i=;i<n/;++i)
{
for(int j=i;j<n--i;++j)
{
int temp=matrix[i][j];
matrix[i][j]=matrix[n--j][i];
matrix[n--j][i]=matrix[n--i][n--j];
matrix[n--i][n--j]=matrix[j][n--i];
matrix[j][n--i]=temp;
}
} }
};

方法二:参考Grandyang博客中方法三。其大体的思路是,先求原矩阵的转置矩阵,然后反正转置矩阵中的每一行的元素,即可。转置矩阵:把矩阵A的行换成相应的列,得到的新矩阵称为A的转置矩阵,记作AT 。转置矩阵的得到的方式是如下图所示,沿对角线,绿色箭头两端的元素对调,得到转置矩阵,然后每一行的元素反转,得到最后要求的矩阵。

代码如下:

 class Solution {
public:
void rotate(vector<vector<int> > &matrix)
{
int n = matrix.size();
for (int i = ; i < n; ++i)
{
for (int j = i + ; j < n; ++j)
{
swap(matrix[i][j], matrix[j][i]);
}
reverse(matrix[i].begin(), matrix[i].end());
}
}
};

[Leetcode] rotate image 旋转图片的更多相关文章

  1. [LeetCode] Rotate List 旋转链表

    Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...

  2. [LeetCode] Rotate Array 旋转数组

    Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array  ...

  3. [LeetCode] Rotate Function 旋转函数

    Given an array of integers A and let n to be its length. Assume Bk to be an array obtained by rotati ...

  4. [LeetCode] Rotate String 旋转字符串

    We are given two strings, A and B. A shift on A consists of taking string A and moving the leftmost ...

  5. rotate.js实现图片旋转 (chrome,IE,firefox都可以实现)

    找了好多资料,要么是IE可以用,但是谷歌不行,,还有就是两个都可以用的,图片大小显示不全.终于找到一个好一点的js,先贴一下代码. 1.rotate.js jQuery.fn.rotate = fun ...

  6. LeetCode 61:旋转链表 Rotate List

    ​给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. Given a linked list, rotate the list to the right by k pla ...

  7. [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 ...

  8. [LeetCode] Rotate Image 旋转图像

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

  9. CSS3 transform rotate(旋转)锯齿的解决办法

    -moz-transform: rotate(5deg);-webkit-transform: rotate(5deg); 把图片旋转了5度.本以为轻而易举,可遇到了问题.在Fireofx中显示正常, ...

随机推荐

  1. php数组常用函数总结

    数组的创建 $arr1 = [ "姓名" => "张三", "籍贯" => "上海", "年龄&q ...

  2. PHP实现SMTP邮件的发送实例

    当你还在纠结php内置的mail()函数不能发送邮件时,那么你现在很幸运,此时的这篇文章可以帮助到你! php利用smtp类来发邮件真是屡试不爽,我用过很久了,基本上没出过问题.本博客后台,当博主回复 ...

  3. Promise 的基础用法

    Promise 的含义 Promise 是异步编程的一种解决方案,比传统的解决方案–回调函数和事件--更合理和更强大.它由社区最早提出和实现,ES6将其写进了语言标准,统一了语法,原生提供了Promi ...

  4. 某CTF收集的Mysql爆表、爆字段语句

    Mysql特性 获取数据库名未知函数可爆数据库名 FUNCTION youcanneverfindme17.a does not exist 获取表名and linestring(pro_id)    ...

  5. ctf题目writeup(1)

    2019/1/28 题目来源:爱春秋 https://www.ichunqiu.com/battalion?t=1 1. 该文件是一个音频文件: 首先打开听了一下,有短促的长的....刚开始以为是摩斯 ...

  6. Python自动化运维——IP地址处理模块

    Infi-chu: http://www.cnblogs.com/Infi-chu/ 模块:IPy 功能:辅助我们高效的完成IP的规划工作 安装: wget https://pypi.python.o ...

  7. HyperLedger Fabric 1.4 超级账本起源(5.1)

    至比特币开源以来,无数技术人员对其进行研究,并且对该系统经过了无数次改进,超级账本项目(Hyperledger)最初也是用来改善比特币的底层技术,最终由Linux基金会组织发展起来.       开放 ...

  8. python2.7练习小例子(五)

        5):题目:输入三个整数x,y,z,请把这三个数由小到大输出.     程序分析:我们想办法把最小的数放到x上,先将x与y进行比较,如果x>y则将x与y的值进行交换,然后再用x与z进行比 ...

  9. CCS实例,网页栏目

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  10. javascript的优美与鸡肋

    --总结来自:<javascript语言精粹> 任何语言都有其优美的地方和其鸡肋的地方.避归一些语言的糟粕,能相应的降低bug出现的几率. 优美处: 函数是头等对象 基于原型继承的动态对象 ...