[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中显示正常, ...
随机推荐
- php面向对象基础知识整理之类中的属性和方法的使用
<?php /** * class Index * 类包含什么 * 1.创建类 * 2.类的属性和类中方法 * 3.类中访问修饰符 * 4.类的封装.继承.多态 */ // 创建类,创建的类名是 ...
- docker配置与实践#可以好好看看
Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从 Apache2.0 协议开源.Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级.可移植的容器中,然后发布到任何流行的 L ...
- 链栈的c++实现
2013-08-30 20:58 1876人阅读 评论(0) 收藏 举报 链栈是借用单链表实现的栈.其不同于顺序栈之处在于: 1.链栈的空间是程序运行期间根据需要动态分配的,机器内存是它的上限.而顺序 ...
- python2.7入门---SMTP发送邮件
SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式.python的smtplib提 ...
- Git使用之一:创建仓储和提交文件
一.前期工作: 1.准备自己的文件夹用于同步文件 2.准备自己的Git账号,并设置好项目(推荐使用国产的码云) 3.安装Git软件 (下载地址: 32-bit Git for Window ...
- Java:static的作用分析
static表示“静态”或者“全局”的意思,但在Java中不能在所有类之外定义全局变量,只能通过在一个类中定义公用.静态的变量来实现一个全局变量. 一.静态变量 1. Java中存在两种变量,一种是s ...
- Java中的原生数据类型
Java中的原生数据类型(Primitive DataType)共有8种: 1)整型: 使用int表示(32位).2)字节型: 使用byte表示(从-128到127之间的256个整数).3)短 ...
- jmeter操作JDBC
1. 依次添加计划.线程组.JDBC Connection Configuration.JDBC Request.HTTP请求.Debug Sampler.察看结果树 在计划中导入mysql的jdbc ...
- 【廖雪峰老师python教程】day1
主要内容摘要 函数参数[个人感觉难度很大,却很重要,可以先大概记一记]不要用的太复杂.戳这儿温习 递归函数:使用递归函数的优点是逻辑简单清晰,缺点是过深的调用会导致栈溢出.重点:找到递归关系和终止条件 ...
- Centos6 使用yum快速搭建LAMP环境
1.安装Apache [root@localhost ~]# yum -y install httpd # 开机自启动 [root@localhost ~]# chkconfig httpd on ...