题目

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. java cpu使用率高异常排查

    1.top命令对cpu进行排序shift+p 2.pwdx pid查找业务进程路径 3.top -Hp pid查看相关负载线程pid 4.printf “0x%x\n” 线程pid     // 将线 ...

  2. HDU 1114 Piggy-Bank 猪仔储钱罐(完全背包)

    题意: 给定一个存钱罐中要存硬币,知道空罐的重量和欲装满的重量,是否能装入?若能,打印最小价值.(注:能装的硬币重量一定刚刚好,里面的总价值要达到最小) 输入: 包含了T个测试例子,在第一行给出.接下 ...

  3. linux 命令——52 ifconfig(转)

    许多windows非常熟悉ipconfig命令行工具,它被用来获取网络接口配置信息并对此进行修改.Linux系统拥有一个类似的工具,也就是ifconfig(interfaces config).通常需 ...

  4. ELF文件的格式和加载过程

    http://blog.csdn.net/lingfong_cool/article/details/7832896 (一) ELF 文件的格式       ELF 文件类型 (1) 可重定位文件(  ...

  5. RHEL7 本地yum源配置

    配置yum 源 1.挂载DVD光盘到/mnt   因为配置时候路径名里面不能有空格,否则不能识别  [root@ mnt]# mount /dev/cdrom /mnt 2.在目录/etc/yum.r ...

  6. 快速排序算法思路分析和C++源代码(递归和非递归)

    快速排序由于排序效率在同为O(N*logN)的几种排序方法中效率较高,因此经常被采用,再加上快速排序思想----分治法也确实实用,因此很多软件公司的笔试面试喜欢考这个. 快速排序是C.R.A.Hoar ...

  7. 扩展 -------jQuery

    本文摘要:http://www.liaoxuefeng.com/ 编写jQuery插件 为了满足需求,我们经常会调用一些插件,js插件都是别人写的,今天就来了解了解一些方法. 给jQuery对象绑定一 ...

  8. redis redis-cli 操作指令

    默认选择 db库是 0 redis-cli -p 6379   查看当前所在“db库”所有的缓存key redis 127.0.0.1:6379> keys *   选择 db库 redis 1 ...

  9. python 用requests请求,报SSL:CERTIFICATE_VERIFY_FAILED错误

    https://www.aliyun.com/jiaocheng/437481.html

  10. linux系统快捷键使用

    本文记录linux系统中快捷键的使用 Ctrl + l 清屏,相当于clear命令Ctrl + o 执行当前命令,并重新显示本命令Ctrl + s 阻止屏幕输出,锁定Ctrl + q 允许屏幕输出Ct ...