[Leetcode] rotate image 旋转图片
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 旋转图片的更多相关文章
- [LeetCode] Rotate List 旋转链表
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- [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 ...
- [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 ...
- [LeetCode] Rotate String 旋转字符串
We are given two strings, A and B. A shift on A consists of taking string A and moving the leftmost ...
- rotate.js实现图片旋转 (chrome,IE,firefox都可以实现)
找了好多资料,要么是IE可以用,但是谷歌不行,,还有就是两个都可以用的,图片大小显示不全.终于找到一个好一点的js,先贴一下代码. 1.rotate.js jQuery.fn.rotate = fun ...
- LeetCode 61:旋转链表 Rotate List
给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. Given a linked list, rotate the list to the right by k pla ...
- [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 ...
- [LeetCode] Rotate Image 旋转图像
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- CSS3 transform rotate(旋转)锯齿的解决办法
-moz-transform: rotate(5deg);-webkit-transform: rotate(5deg); 把图片旋转了5度.本以为轻而易举,可遇到了问题.在Fireofx中显示正常, ...
随机推荐
- wamp环境下安装imagick扩展
先上图,如下是安装成功后的phpinfo()界面: 安装步骤: 1.先确定安装版本,比如我的的php : php7.0.12 x86 ts 那么就需要三方版本 要一致:imagick软件本身( 如x ...
- hadoop生态搭建(3节点)-06.hbase配置
# http://archive.apache.org/dist/hbase/1.2.4/ # ==================================================== ...
- STM32(7)——通用定时器PWM输出
1.TIMER输出PWM基本概念 脉冲宽度调制(PWM),是英文“Pulse Width Modulation”的缩写,简称脉宽调制,是利用微处理器的数字输出来对模拟电路进行控制的一种 ...
- CMDB介绍
CMDB https://lupython.gitee.io/2018/05/05/CMDB%E4%BB%8B%E7%BB%8D/ 尚泽凯博客地址 传统运维与自动化运维的区别 传统运维: 1.项目 ...
- ruby Encoding
一. 查看ruby支持的编码 Encoding.name_list 二. 搜索编码 Encoding.find('US-ASCII') #=> US-ASCII,不存在则抛出异常 三. __EN ...
- Redis 在springBoot中的一个使用示例
在现系统中使用了一个字典表,更新或插入字典表需要做Redis缓存 @Override @Cache(name = Constants.REDIS_PREFIX_DIC, desc = "变更 ...
- nodejs环境变量配置
步骤 创建文件夹:安装包 配置环境变量: export NODE_HOME=/root/安装包/node-v7.6.0-linux-x64 export PATH=$NODE_HOME/bin:$PA ...
- Java8新特性(三)——Optional类、接口方法与新时间日期API
一.Optional容器类 这是一个可以为null的容器对象.如果值存在则isPresent()方法会返回true,调用get()方法会返回该对象. 查看结构图可以看到有如下常用方法: of(T)—— ...
- Spring MVC - URL路径映射
1. 普通映射 A. @RequestMapping("/test1") B. @RequestMapping(value={"/test1", "/ ...
- find的详细使用
对我我这个出学者,这个已经算是很难了,不过今天整理了一下,感觉还可以接受. find Linux中十分重要的一个查找功能, [root@moban /]# find /tmp/ -type f -na ...