题目

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. python字符串及字符串操作

    字符串介绍 1.字符串在内存中的存储: 2.字符串相加: 3.字符串的格式化: In [1]: a = 100 In [2]: a Out[2]: 100 #100<255,在堆内存下占用了一个 ...

  2. mybatis-动态sql1

    在多条件查询的情况下必须用到动态sql 沿用之前的项目 1.在dao中添加多添件查询方法 package com.java1234.mappers; import java.util.List;imp ...

  3. Linux最常用命令实战

    1.改变机器的名称: vim /etc/hostname Master 在文件中修改机器名称为我们想要的名称(相当于域名) 可以通过shutdown -h now 关闭 2.查看当前机器IP: ifc ...

  4. 禁止windows自动更新后重新启动

    运行gpedit.msc: 按照下图操作: 参考:http://www.xitongcheng.com/jiaocheng/win7_article_94.html

  5. 8. String to Integer

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  6. linux 命令——17 whereis(转)

    whereis命令只能用于程序名的搜索,而且只搜索二进制文件(参数-b).man说明文件(参数-m)和源代码文件(参数-s).如果省略参数,则返回所有信息. 和 find相比,whereis查找的速度 ...

  7. 打造颠覆你想象中的高性能,轻量级的webform框架---js直接调后台的封装(第三天)

    如果你没有看我第二天写的内容的,我想你是看不懂的!!!! 好了,废话不多说,怎么才能让我们的代码变得牛逼起来呢?怎么封装我们的代码呢?我们不可能 每个页面都需要那样写吧,那我们来一步一步来封装 我们的 ...

  8. 显示 Mac隐藏的文件夹 命令语句

    默认情况下,模拟器的目录是隐藏的,要想显示出来,需要在Mac终端输入下面的命令 显示Mac隐藏文件的命令:defaults write com.apple.finder AppleShowAllFil ...

  9. 【BZOJ2002】[HNOI2010] 弹飞绵羊(大力分块)

    点此看题面 大致题意: 有\(n\)个弹力装置,当到达第\(i\)个装置时,会被弹到第\(i+k_i\)个装置,若不存在第\(i+k_i\)个装置,就会被弹飞.有两种操作,一种操作是将\(k_x\)改 ...

  10. 运维如何延续自己的职业生涯--萧田国2017年GOPS深圳站演讲内容

    正如 萧田国在2017年GOPS深圳站演讲所提及的,运维职业生涯规划,应该是T字型. 关于指导原则,正如腾讯好友@赵建春所言: 如果一个领域不能做到TOP,那就是一种伤害. 运维在编程.开发领域,能做 ...