【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 ...
随机推荐
- slfj4 + logback
slf4j:(Simple Logging Facade for Java,简单日志门面),它不是具体的日志解决方案,只服务于各种各样的日志系统.在使用SLF4J的时候,不需要再代码中或配置文件中指定 ...
- 根据accept-language自动设置UICulture和Culture
在web.config中添加如下配置: <system.web> <globalization uiCulture="auto" culture="au ...
- Java JDBC链接Oracle数据库
package com.test.test; import java.io.FileInputStream;import java.io.FileNotFoundException;import ja ...
- VMware下Centos6.4安装
VMware(Virtual Machine ware)是一个“虚拟PC”软件公司,提供服务器.桌面虚拟化的解决方案. 小伙伴们网上下载VMware11,一路下一步自己安装吧!!! 打开 VMware ...
- maven+jenkins发布环境
安装java省略,下面是环境变量 export JAVA_HOME=/usr/java/jdk1.8.0_65/ export PATH=$JAVA_HOME/bin:$PATH export CLA ...
- 如何处理CloudFoundry应用部署时遇到的254错误
使用SAP云平台的CloudFoundry部署应用: 在cockpit遇到错误信息:instance: a0abe2b5-7623-4cf1-4c65-0c79, index: 0, exit_des ...
- NOIP2018提高组Day2 解题报告
前言 关于\(NOIP2018\),详见此博客:NOIP2018学军中学游记(11.09~11.11). \(Day2\)的题目和\(Day1\)比起来,真的是难了很多啊. \(T1\):旅行(点此看 ...
- 【洛谷3157】[CQOI2011] 动态逆序对(CDQ分治)
点此看题面 大致题意: 给你一个从\(1\)到\(n\)的排列,问你每次删去一个元素后剩余的逆序对个数. 关于\(80\)分的树套树 为了练树套树,我找到了这道题目. 但悲剧的是,我的 线段树套\(T ...
- python_45_目录编程
#获取当前目录 import os print(os.getcwd()) #获取目录内容 import os print(os.listdir('C:\\Python27')) #创建目录 impor ...
- PAT (Basic Level) Practise (中文)- 1002. 写出这个数 (20)
http://www.patest.cn/contests/pat-b-practise/1002 读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式:每个测试输入包含1个 ...