a common method to rotate the image

 1 /*
 2  * clockwise rotate
 3  * first reverse up to down, then swap the symmetry
 4  * 1 2 3     7 8 9     7 4 1
 5  * 4 5 6  => 4 5 6  => 8 5 2
 6  * 7 8 9     1 2 3     9 6 3
 7 */
 8 void rotate(vector<vector<int> > &matrix) {
 9     reverse(matrix.begin(), matrix.end());
10     for (int i = 0; i < matrix.size(); ++i) {
11         for (int j = i + 1; j < matrix[i].size(); ++j)
12             swap(matrix[i][j], matrix[j][i]);
13     }
14 }
15
16 /*
17  * anticlockwise rotate
18  * first reverse left to right, then swap the symmetry
19  * 1 2 3     3 2 1     3 6 9
20  * 4 5 6  => 6 5 4  => 2 5 8
21  * 7 8 9     9 8 7     1 4 7
22 */
23 void anti_rotate(vector<vector<int> > &matrix) {
24     for (auto vi : matrix) reverse(vi.begin(), vi.end());
25     for (int i = 0; i < matrix.size(); ++i) {
26         for (int j = i + 1; j < matrix[i].size(); ++j)
27             swap(matrix[i][j], matrix[j][i]);
28     }
29 }

这边有一个题目链接可以练习。

时间: 2024-08-10 23:26:32

a common method to rotate the image的相关文章

CareerCup之1.6 Rotate Image

[题目] 原文: 1.6 Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees. Can you do this in place? 译文: 一张图像表示成NxN的矩阵,图像中每个像素是4个字节,写一个函数把图像旋转90度. 你能原地进行操作吗?(即不开辟额外的存储空间) [分析]

[CareerCup] 1.6 Rotate Image 翻转图像

1.6 Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees. Can you do this in place? LeetCode中的原题,请参见我之前的博客Rotate Image 旋转图像.

Cracking the coding interview汇总目录

很久之前刷的CTCI的题目,都快忘记了,做个分类汇总,再重新好好复习一遍. Chapter 1 | Arrays and Strings 1.1 Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures? 1.2 Write code to reverse a C-Style String. (C-Str

Spring Security入门Demo

一.spring Security简介 SpringSecurity,这是一种基于Spring AOP和Servlet过滤器的安全框架.它提供全面的安全性解决方案,同时在Web请求级和方法调用级处理身份确认和授权.在Spring Framework基础上,Spring Security充分利用了依赖注入(DI,Dependency Injection)和面向切面技术. 二.建立工程 参考http://blog.csdn.net/haishu_zheng/article/details/51490

spring security+mybatis+springMVC构建一个简单的项目

1.引用 spring security ,这是一种基于spring AOP和Servlet的过滤安全框架.它提供全面的安全性解决方案,同时在web请求级和方法的调用级处理身份确认和授权.在spring framework基础上,spring security充分利用了依赖注入(DI,Dependency Injection)和AOP技术. 下面就让我们用一个小的晓得项目来出初步了解Spring Security 的强大功能吧. 2.项目实战    1)项目的技术架构:maven+spring

【转载】Discriminative Learning和Generative Learning

Discriminative Learning和Generative Learning 2011-12-08 10:47 1929人阅读 评论(2) 收藏 举报 variablesdependencies算法includeparametersexpress Discriminative 学习算法是一类模型化输入(X)输出(Y)的关系的方法,简单来说就好比中医,我们只知道用若干个药(当归,虎骨...)可以凑成一个药方,就能治疗跌打病痛.但我们并不去了解内部的原因,我们将其看做一个黑盒,只需了解X和

system strategies of Resources Deadlock

In computer science, Deadlock is a naughty boy aroused by compete for resources. Even now, there isn't a valid method to deal with it. This is amazing. You know, we have many excellent scientists,  not all of issues can fight with us so many years. F

[转]FluentData

本文来自:http://fluentdata.codeplex.com/wikipage?title=Fluency&referringTitle=Home Documentation  Fluency This contribution is an attempt to speedup the development of domain layer in bottom to top model with FluentData. This means you already have the D

JAVA 【引用类型】和【对象类型】在【继承】中的异同

介绍 JAVA [引用类型]和[对象类型]在[继承]中的异同.这个问题自己整理过N次.也被人当菜鸟问过N次.所以,在此简单整理一下.以供大家分享. 在继承关系中.一般成员变量是依据引用类型 在继承关系中.静态成员变量是依据引用类型 在继承关系中,一般方法是依据对象类型 在继承关系中,静态方法是依据引用类型 注意 静态成员变量,静态方法是基于类的,本文为了測试观察.所以.会用对象去引用静态成员变量和静态方法. Super Class: package shuai.study.inherit; pu