题目:

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

Rotate the image by 90 degrees (clockwise). (Medium)

Follow up:
Could you do this in-place?

分析:

就是在纸上画一画找到对应关系即可, newMatrix[i][j] = matrix[n - 1 - j][i];

所以简单的方法就是开一个新数组,按照对应关系填入新数组,再拷贝回原来的数组。

代码:

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

题目中的follow-up,要in-place,考虑原数组上如何处理。

要在原数组上处理局部交换感觉就不好想了(会交换完丢元素),把数组整体一起处理好一些。

再观察对应关系,只需要横纵坐标先颠倒,然后纵坐标上下翻转即可。

代码:

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

LeetCode48 Rotate Image的更多相关文章

  1. Leetcode48. Rotate Image旋转图像

    给定一个 n × n 的二维矩阵表示一个图像. 将图像顺时针旋转 90 度. 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵.请不要使用另一个矩阵来旋转图像. 示例 1: 给定 m ...

  2. [Swift]LeetCode48. 旋转图像 | Rotate Image

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

  3. leetcode-48.旋转图像

    leetcode-48.旋转图像 point: 数组 题意 给定一个 n × n 的二维矩阵表示一个图像. 将图像顺时针旋转 90 度. 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维 ...

  4. Canvas绘图之平移translate、旋转rotate、缩放scale

    画布操作介绍 画布绘图的环境通过translate(),scale(),rotate(), setTransform()和transform()来改变,它们会对画布的变换矩阵产生影响. 函数 方法 描 ...

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

  6. [LeetCode] Rotate List 旋转链表

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

  7. [LeetCode] Rotate Image 旋转图像

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

  8. jQuery.rotate.js参数

    CSS3 提供了多种变形效果,比如矩阵变形.位移.缩放.旋转和倾斜等等,让页面更加生动活泼有趣,不再一动不动.然后 IE10 以下版本的浏览器不支持 CSS3 变形,虽然 IE 有私有属性滤镜(fil ...

  9. CSS3属性transform详解之(旋转:rotate,缩放:scale,倾斜:skew,移动:translate)

    CSS3属性transform详解之(旋转:rotate,缩放:scale,倾斜:skew,移动:translate)   在CSS3中,可以利用transform功能来实现文字或图像的旋转.缩放.倾 ...

随机推荐

  1. 第三百三十九天 how can I 坚持

    脑子里老是无缘无故浮现出之前学的古文,之前只是傻学了,什么都没搞懂啊. 吾师道也,夫庸知其年之先后生于吾乎?是故无贵无贱,无长无少,道之所存,师之所存也. 是故弟子不必不如师,师不必贤于弟子,闻道有先 ...

  2. JXTA中定义自己的成员服务

    http://blog.csdn.net/neusoftware_20063500/article/details/4302903 —————————————————————————————————— ...

  3. HDU 4602 Magic Ball Game(离线处理,树状数组,dfs)

    Magic Ball Game Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  4. Spring中的BeanUtils与apache commons中的BeanUtils用法[1]

    1. 前言 在开发过程中,经常遇到把要给一个bean的属性赋给另外一个bean.最笨的方法是每个属性都单独写一个,聪明的方法是应用反射写一个工具方法.考虑到这个需求基本每个程序员都会遇到,那么一定已经 ...

  5. (算法)N皇后问题

    题目: 八皇后问题:在8 X 8的国际象棋上摆放八个皇后,使其不能相互攻击,即任意两个皇后不得处于同一行,同一列或者同意对角线上,求出所有符合条件的摆法. 思路: 1.回溯法 数据结构: 由于8个皇后 ...

  6. CloudStack 4.2 与CloudStack 4.1二级存储API发生变化

    CloudStack 4.1查看二级存储 http://192.168.150.16:8080/client/api?command=listHosts&response=json&s ...

  7. Find n‘th number in a number system with only 3 and 4

    这是在看geeksforgeeks时看到的一道题,挺不错的,题目是 Given a number system with only 3 and 4. Find the nth number in th ...

  8. Ubuntu ENet 的下载和编译

    ENet的目的是提供一个相对轻便.简单和强大的网络通信层的UDP(用户数据报协议). 它提供的主要功能是可选的.可靠的.顺序的数据包发送. ENet省略了一些更高层次的网络功能,如身份验证.加密,尤其 ...

  9. Linux错误代码

    #ifndef _I386_ERRNO_H #define _I386_ERRNO_H #define EPERM 1 /* Operation not permitted */ #define EN ...

  10. ARM体系架构下的同步操作

    http://blog.hamobai.com/2012/06/28/synchronization-on-ARM-one/ 处理器在访问共享资源时,必须对临界区进行同步,即保证同一时间内,只有一个对 ...