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?
The rotation can be performed in layers, where you perform a cyclic swap on the edges on
each layer In the frst for loop, we rotate the frst layer (outermost edges) We rotate the
edges by doing a four-way swap frst on the corners, then on the element clockwise from the
edges, then on the element three steps away
Once the exterior elements are rotated, we then rotate the interior region’s edge
class Solution {
public:
void rotate(vector<vector<int> > &matrix) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int n = matrix.size();
if(n < ) return ;
for(int lay = ; lay < n/; lay ++){
int first = lay ;
int last = n - - lay;
for(int i = first; i< last; i++){
int offset = i - first;
//store the top
int top = matrix[first][i] ;
//left to top
matrix[first][i] = matrix[last - offset][first];
//bottom to left
matrix[last - offset][first] = matrix[last][last- offset];
//right to bottom
matrix[last][last - offset] = matrix[i][last];
//top to right
matrix[i][last] = top;
}
}
}
};
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 ...
随机推荐
- zabbix 启用分区表后需要关闭Housekeeper
<pre name="code" class="html">Zabbix Housekeeper changes: 使用分区表需要关闭zabbix的 ...
- js深入研究之函数内的函数
第一种 function foo() { ; function bar() { a *= ; } bar(); return a; } 第二种 function foo() { ; function ...
- Java 内存区域和GC机制-java概念理解
推荐几篇关于java内存介绍的文章 Java 内存区域和GC机制 http://www.cnblogs.com/hnrainll/archive/2013/11/06/3410042.html ...
- Android studio无法更新 提示网络连接失败
Android studio 更新时,提示网络问题 “Connection failed. Please check your network connection and try again” 在默 ...
- #python基础学习模块:marshal 对象的序列化
#标准库地址:https://docs.python.org/2/library/marshal.html"""有时候,要把内存中一个对象持久化保存磁盘或者序列化二进制流 ...
- PHP设计模式笔记八:原型模式 -- Rango韩老师 http://www.imooc.com/learn/236
原型模式 概述: 1.与工厂模式作用类似,都是用来创建对象 2.与工厂模式的实现不同,原型模式是先创建好一个原型对象,然后通过clone原型对象来创建新的对象,这样就免去了类创建时重复的初始化操作 3 ...
- 域名地址默认跳转到www(301重定向)
要做这个操作之前,你首先必须肯定要有一个域名..... 然后域名指向了某一个外网主机地址,能正常访问网站 IIS7之后版本的看客继续往下看,IIS7之前的版本,请止步,我没有对之前的版本做过 首先确认 ...
- 图像重采样(CPU和GPU)
1 前言 之前在写影像融合算法的时候,免不了要实现将多光谱影像重采样到全色大小.当时为了不影响融合算法整体开发进度,其中重采样功能用的是GDAL开源库中的Warp接口实现的. 后来发现GDAL War ...
- Java数据结构漫谈-Vector
List除了ArrayList和LinkedList之外,还有一个最常用的就是Vector. Vector在中文的翻译是矢量,向量,所以大家喜欢把Vector叫做矢量数组,或者向量数组. 其实就底层实 ...
- Java数据结构漫谈-ArrayList
ArrayList是一个基于数组实现的链表(List),这一点可以从源码中看出: transient Object[] elementData; // non-private to simplify ...