【Rotate Image】cpp
题目:
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的更多相关文章
- 【Rotate List】cpp
题目: Given a list, rotate the list to the right by k places, where k is non-negative. For example:Giv ...
- hdu 4739【位运算】.cpp
题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...
- 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~ ...
- leetcode 【Rotate List 】python 实现
题目: Given a list, rotate the list to the right by k places, where k is non-negative. For example:Giv ...
- 【Valid Sudoku】cpp
题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- leetcode 【 Rotate Image 】python 实现
题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...
- 【Permutations II】cpp
题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...
- 【Subsets II】cpp
题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...
- 【Sort Colors】cpp
题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
随机推荐
- GitLab关于SSH的使用
SSH Git是分布式版本控制系统,这意味着您可以在本地工作,但您也可以将更改共享或“推送”到其他服务器.在将更改推送到GitLab服务器之前,您需要一个用于共享信息的安全通信通道. SSH协议提供此 ...
- LAMP stack-5.6.22 (OpenLogic CentOS 7.2)
平台: CentOS 类型: 虚拟机镜像 软件包: apache2.4.20 mysql5.6.30 php5.6.22 apache application server basic softwar ...
- Win10 应用商店管理应用
在企业日常办公中,对 Windows 10 应用商店软件不需要,希望办公系统干净一些.企业运维中,我们可以使用组策略来管理Windows 10 微软Store应用程序.可以根据组织的要求进行配置,多项 ...
- 杭电acm刷题顺序
最近兴趣来了,闲暇之余,回顾大学期间刷过的杭电acm那些入门级别的题,以此巩固基础知识! 以下参考刷题顺序,避免入坑 原文传送门:https://blog.csdn.net/liuqiyao_01/a ...
- Yii2 widgets [mztest/yii2-widget-file-upload]
Enjoy it. A widget for uploading files to your server. Github , Packagist Screenshots
- jq动态增加的button标签click回调失效的问题,即动态增加的button标签绑定事件$("button.class").click(function)无效
对于新增加的页面元素,改变了页面结构,如果是使用老办法$("button.class").click(function)去监听新的button标签事件,会失效. 笔者的应用是文字的 ...
- HDU 1712 ACboy needs your help AC男需要你的帮助 (分组的背包)
分组背包问题:有N件物品和一个容量为V的背包.第i件物品的体积是c[i],价值是w[i].这些物品被划分为若干组,每组中的物品互相冲突,最多选一件.求解将哪些物品装入背包可使这些物品的体积总和不超过背 ...
- php使用GD库实现图片水印和缩略图——封装成类
学完了如何使用GD库来实现对图片的各种处理,那么我们可以发现,不管哪种方法,都有相似之处,如果我们把这些相似的地方和不相似的地方都封装成类,这样就可以提升代码的速度,而且节省了很多时间,废话不多说,来 ...
- coursera 算法二 week 1 wordnet
这周的作业可谓是一波三折,但是收获了不少,熟悉了广度优先搜索还有符号图的建立.此外还知道了Integer.MAX_VALUE. SAP: 求v和w的大概思路是对v和w分别广度优先搜索,然后遍历图中每一 ...
- IBM中国
https://www.ibm.com/developerworks/cn/linux/l-memory/