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?

思路:

每次转着圈挪四个元素,这步只需要一个int的额外空间,想象一个方阵

哦不好意思,这个太不专业了,咱换个大一点6×6的

草图上比划比划第二层的元素怎么移动,一次Accept的感觉好爽

代码:

 void rotate(vector<vector<int> > &matrix) {
int n = matrix.size();
if(n < ) return; for(int layer = ; layer < n/; layer++){
for(int i = layer; i < n--layer; i++){//避免重复处理n-1-layer
int leftTop = matrix[layer][i];//注意二维矩阵的(i,j)index与数学坐标(x,y)中是相反的
matrix[layer][i] = matrix[n--i][layer]; //先在纸上想清楚赋值的先后循序
matrix[n--i][layer] = matrix[n--layer][n--i];
matrix[n--layer][n--i] = matrix[i][n--layer];
matrix[i][n--layer] = leftTop;
}
}
}

【题解】【矩阵】【回溯】【Leetcode】Rotate Image的更多相关文章

  1. C++ STL@ list 应用 (leetcode: Rotate Array)

    STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...

  2. [LeetCode]Rotate Image(矩阵旋转)

    48. Rotate Image     Total Accepted: 69437 Total Submissions: 198781 Difficulty: Medium You are give ...

  3. 【LeetCode】【矩阵旋转】Rotate Image

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

  4. [LeetCode] Rotate Image n-by-n矩阵顺时针旋转

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

  5. [leetcode]Rotate Image @ Python

    原题地址:https://oj.leetcode.com/problems/rotate-image/ 题意: You are given an n x n 2D matrix representin ...

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

  7. [LeetCode] Rotate List 旋转链表

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

  8. [LeetCode] Rotate Image 旋转图像

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

  9. LeetCode Rotate Image (模拟)

    题意: 将一个n*n的矩阵顺时针旋转90度. 思路: 都是差不多的思路,交换3次也行,反转再交换也是行的. class Solution { public: void rotate(vector< ...

  10. LeetCode——Rotate List

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

随机推荐

  1. NoSQL分类

    NoSQL数据库分类: NoSQL DEFINITION:Next Generation Databases mostly addressing some of the points: beingno ...

  2. [示例]NSEnumerator-使用枚举类型实现数组的逆序输出

    代码: #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepo ...

  3. js继承实例

    第一种方法:对象冒充(临时属性) 借用临时属性,指向超类,末了删除 function Person(name,gender){ this.name=name; this.gender=gender; ...

  4. iOS 登陆的实现四种方式

    iOS 登陆的实现四种方式 一. 网页加载: http://www.cnblogs.com/tekkaman/archive/2013/02/21/2920218.ht ml [iOS登陆的实现] A ...

  5. bzoj 2244: [SDOI2011]拦截导弹

    #include<cstdio> #include<iostream> #include<algorithm> #define M 100009 using nam ...

  6. C#获取指定日期为一年中的第几周

    /// <summary> /// 获取指定日期,在为一年中为第几周 /// </summary> /// <param name="dt">指 ...

  7. SQL 插入日期时间 变量值

    --看看吧^^ CREATE TABLE #temp ( test datetime ) go --SQL: INSERT #temp SELECT 1.1 UNION ALL GO --SQL: I ...

  8. plist 和 Xib

    plist文件 mainbudin加载时候有后缀 xib文件  mainbudin加载时候无需后缀

  9. 【转发】Linux系统下安装rz/sz命令及使用说明

    对于经常使用Linux系统的人员来说,少不了将本地的文件上传到服务器或者从服务器上下载文件到本地,rz / sz命令很方便的帮我们实现了这个功能,但是很多Linux系统初始并没有这两个命令.今天,我们 ...

  10. SharePoint 2013 开发——开发并部署webpart

    博客地址:http://blog.csdn.net/FoxDave webpart我们就不详细阐述了,在APP的开发中,自定义属性设置通过APP webpart的URL查询字符串传递,它通过IFR ...