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?

最简单的想法,两重循环。

第一重循环是对于矩阵的层次(从外到内)

第二重循环是对于每一层次的四元素轮换

举例:

1 2 3

4 5 6

7 8 9

最外层:

1-->3-->9-->7

2-->6-->8-->4

最内层:

5

class Solution {
public:
void rotate(vector<vector<int> > &matrix) {
if(matrix.empty())
return;
int n = matrix.size();
int temp;
for(int i = ; i < (n+)/; i ++)
{
for(int j = i; j < n--i; j ++)
{
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;
}
}
}
};

还有一种技巧性比较强的解法。

顺时针旋转90度可以分解为两步:

1、上下颠倒reverse

2、转置transpose

举例:

1 2 3

4 5 6

7 8 9

reverse之后

7 8 9

4 5 6

1 2 3

transpose之后

7 4 1

8 5 2

9 6 3

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

【LeetCode】48. Rotate Image (2 solutions)的更多相关文章

  1. 【LeetCode】48. Rotate Image

    Difficulty:medium  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/rotate-image/ ...

  2. 【LeetCode】48. Rotate Image 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  3. 【LeetCode】61. Rotate List 解题报告(Python)

    [LeetCode]61. Rotate List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...

  4. 【一天一道LeetCode】#48. Rotate Image

    一天一道LeetCode系列 (一)题目 You are given an n x n 2D matrix representing an image. Rotate the image by 90 ...

  5. 【LeetCode】189. Rotate Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 切片 递归 日期 题目地址:https://leet ...

  6. 【LeetCode】189 - Rotate Array

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

  7. 【LeetCode】75. Sort Colors (3 solutions)

    Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...

  8. 【LeetCode】90. Subsets II (2 solutions)

    Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...

  9. 【leetcode】61. Rotate List

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

随机推荐

  1. C语言运算符优先级及结合性

    今天去翻了下C语言运算符的优先级和结合性,发现当初学习的时候就没认真记住,惭愧.发现一篇讲得不错的文章,编辑了下转来供以后翻阅. C语言运算符优先级表(由上至下,优先级依次递减) 运算符 结合性 () ...

  2. 嵌入式linux应用程序调试方法

    嵌入式linux应用程序调试方法 四 内存工具 五 C/C++代码覆盖.性能profiling工具 四 内存工具 您肯定不想陷入类似在几千次调用之后发生分配溢出这样的情形. 许多小组花了许许多多时间来 ...

  3. mysqldump与source

    mysqldump示例 mysqldump --default-character-set=utf8 -d --opt -hlocalhost -uroot -p123456 --where=&quo ...

  4. ffmpeg 2.3版本号, 关于ffplay音视频同步的分析

    近期学习播放器的一些东西.所以接触了ffmpeg,看源代码的过程中.就想了解一下ffplay是怎么处理音视频同步的,之前仅仅大概知道通过pts来进行同步,但对于怎样实现却不甚了解,所以想借助这个机会, ...

  5. cubieboard 通过VGA点亮电脑屏幕笔记

    前题:由于公司某些方面的需要,于是就开始尝试了来通过VGA输出--因为不可能每个地方都是高清电视,这是其一:如果要买一个HDMI转VGA的话,成本上就有所上升:反正吧,各种理由,都觉得直接通过VGA输 ...

  6. [4] 圆锥(Cone)图形的生成算法

    顶点数据的生成 bool YfBuildConeVertices ( Yreal radius, Yreal height, Yuint slices, YeOriginPose originPose ...

  7. C++经典排序算法总结

    转发请注明出处:https://www.cnblogs.com/fnlingnzb-learner/p/9374732.html 最近在研究一些经常用到的东西想把它们做一个汇总,想了想用到最多的应该是 ...

  8. rotate-function

    https://leetcode.com/problems/rotate-function/ public class Solution { public int maxRotateFunction( ...

  9. 理清Processor, Processor Sockets, Processor Cores, Logical Processors, Hyperthreading这些概念吧

    如果你只知道CPU这么一个概念,那么是无法理解CPU的拓扑的.事实上,在NUMA架构下,CPU的概念从大到小依次是:Node.Socket.Core.Logical Processor. 随着多核技术 ...

  10. android系统访问自己的tomcat服务器下的项目不能访问的原因

    今天做android的一个下载功能,用自己机子上的tomcat做服务器,在tomcat上下载东西,可是android系统老是提示错误说不能连接到我的tomcat,可是我明明启动了tomcat服务啊,而 ...