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 ...
随机推荐
- 虚拟机最佳实践:单个 VM、临时存储和已上传磁盘
大家好! 我是 Drew McDaniel,来自 Microsoft Azure虚拟机功能研发团队,我从团队成立之初就已加入. 在本博客文章中,我将分享一些最佳实践指南,帮助您充分利用您的Azure虚 ...
- SQL SERVER 系统存储过程
Transact-SQL中的存储过程,非常类似于Java语言中的方法,它可以重复调用.当存储过程执行一次后,可以将语句缓存中,这样下次执行的时候直接使用缓存中的语句.这样就可以提高存储过程的性能. Ø ...
- 段错误bug的调试
我们在用C/C++语言写程序的时侯,内存管理的绝大部分工作都是需要我们来做的.实际上,内存管理是一个比较繁琐的工作,无论你多高明,经验多丰富,难 免会在此处犯些小错误,而通常这些错误又是那么的浅显而易 ...
- redis 学习笔记二 (简单动态字符串)
redis的基本数据结构是动态数组 一.c语言动态数组 先看下一般的动态数组结构 struct MyData { int nLen; char data[0]; }; 这是个广泛使用的常见技巧,常用来 ...
- join 数据库
早上随手拿了本数据库的书,看到关于join的,想到很久之前妹妹(妹妹离职了,好桑感)发给我的一个简单浅显易懂的关于这方面的网页,所以翻出来瞅瞅,贴出来与大家共享之. http://coolshell. ...
- java之File
1.文件创建.重命名.删除 code: package com.test; import java.io.File; import java.io.IOException; public class ...
- python calendar标准库基础学习
# -*- coding: utf-8 -*-# 作者:新手__author__ = 'Administrator'#标准库:日期时间基础学习:calendar:处理日期#例1import calen ...
- Perl 多线程模块 Parallel::ForkManager
Perl 多线程模块 Parallel::ForkManager 一个简单的并行处理模块.这个是用来对付循环的多线程处理. 放在循环前面. Table of Contents 1 Synops内容简介 ...
- 标准I/O介绍
标准I/O库 1. 标准I/O介绍 不仅是在UNIX系统中,很多操作系统上都实现了标准I/O库. 标准I/O库由ANSI C 标准说明 标准 I/O 库处理很多细节,例如带有缓冲分配.以优化长度执行的 ...
- 学习iOS必须知道的[转载]
part1 : http://www.cocoachina.com/ios/20150608/12052.html part2 : http://www.cocoachina.com/ios/2015 ...