题目

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?

代码

class Solution {
public:
void rotate(vector<vector<int> > &matrix) {
const unsigned int len = matrix.size();
if ( len < ) return;
for ( int row = ; row < len; ++row)
{
for (int col = ; col < len--row; ++col)
{
std::swap(matrix[row][col], matrix[len--col][len--row]);
}
}
for ( int row = ; row < len/; ++row)
{
for ( int col = ; col < len; ++col)
{
std::swap(matrix[row][col], matrix[len--row][col]);
}
}
}
};

Tips:

1. 算法:先沿着副对角线(右上到左下)swap元素;再沿着水平中轴线swap元素

2. 注意循环遍历时的结束条件,不要做无谓的遍历

=============================================

图像旋转90°的算法技巧:先副对角线对折;再沿着水平中轴对折。代码一次AC。

class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
const int N = matrix.size();
if ( N< ) return;
for ( int i=; i<N; ++i )
{
for ( int j=; j<N-i; ++j )
{
std::swap(matrix[i][j], matrix[N--j][N--i]);
}
}
for ( int i=; i<N/; ++i)
{
for ( int j=; j<N; ++j)
{
std::swap(matrix[i][j], matrix[N--i][j]);
}
}
}
};

【Rotate Image】cpp的更多相关文章

  1. 【Rotate List】cpp

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

  2. hdu 4739【位运算】.cpp

    题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...

  3. Hdu 4734 【数位DP】.cpp

    题意: 我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~ ...

  4. leetcode 【Rotate List 】python 实现

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

  5. 【Valid Sudoku】cpp

    题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  6. leetcode 【 Rotate Image 】python 实现

    题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...

  7. 【Permutations II】cpp

    题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...

  8. 【Subsets II】cpp

    题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...

  9. 【Sort Colors】cpp

    题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...

随机推荐

  1. <Android 应用 之路> 天气预报(五)

    前言 写了上一篇文章,讲了下这个实现天气预报的使用内容,现在又到了看代码的时候,主要还是贴代码,然后添加足够的注释. 聚合数据SDK配置 将juhe_sdk_v_X_X.jar以及armeabi文件夹 ...

  2. 重置SQLSERVER表的自增列,让自增列重新计数【转】

    很多时候我们需要重置某个表的自增列,让自增列重新从1开始记数.最蠢的方法当然是把该表删掉再重新建表了.其实,还有其它的方法可以重置自增列的值: 方法一:使用TRUNCATE TABLE语句: TRUN ...

  3. 实战:ADFS3.0单点登录系列-集成SharePoint

    这是本系列第四篇了,终于轮到SharePoint上场了,但是本文不会过多讲解SharePoint安装等话题,而是直入主题,讲解如何进行配置,让其于ADFS配合完成SSO的工作. 注意:本文使用的Sha ...

  4. 基于PowerShell的Lync Server管理 使用C# 之 Telephony 功能 查看 /修改

    本以为这个属性可以在用户信息中直接反应出来,但是看了好几遍还是没找到这个属性名称 这个功能没有在get-User 的结果中直接反映出来 但是可以通过 Property 查找单个选项 如: Get-Cs ...

  5. java Vamei快速教程16 RTTI

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 运行时类型识别(RTTI, Run-Time Type Identificatio ...

  6. IOS view拖拽(触摸事件)

    • iOS中的事件可以分为3大类型 触摸事件 加速计事件 远程控制事件 响应者对象 • 在iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并处理事 件.我们称之为“响应 ...

  7. NUMA的原理与局限

    为什么要有NUMA 在NUMA架构出现前,CPU欢快的朝着频率越来越高的方向发展.受到物理极限的挑战,又转为核数越来越多的方向发展.如果每个core的工作性质 都是share-nothing(类似于m ...

  8. java编程基础——从上往下打印二叉树

    题目描述 从上往下打印出二叉树的每个节点,同层节点从左至右打印. 题目代码 /** * 从上往下打印出二叉树的每个节点,同层节点从左至右打印. * Created by YuKai Fan on 20 ...

  9. SummerVocation_Leaning--java动态绑定(多态)

    概念: 动态绑定:在执行期间(非编译期间)判断所引用的对象的实际类型,根据实际类型调用其相应的方法.如下例程序中,根据person对象的成员变量pet所引用的不同的实际类型调用相应的方法. 具体实现好 ...

  10. Java - 若try中有return语句,finally会执行吗?在return之前还是之后呢?

    会执行,在方法return动作之前,return语句执行之后,若finally中再有return语句,则此方法以finally的return作为最终返回,若finally中无return语句,则此方法 ...