【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 ...
 
随机推荐
- [荐]推荐一个shell学习的网站
			
最近再用shell脚本,发现一个脚本学习的网站,非常好用,特此推荐一下. shell学习网站链接:http://c.biancheng.net/cpp/shell/
 - 跨平台移动开发phonegap/cordova 3.3全系列教程-目录
			
目录(更新完成后会附上源码供参考) 第一章 android平台开发 phonegap/cordova简介 1.开发环境搭建 2.helloworld 3.启动画面 4.结合asp.net/jqmboi ...
 - C#问题记录-CallbackOnCollectedDelegate
			
做项目的时候遇到了这个问题: 检测到:CallbackOnCollectedDelegate 对“xx.HookProc::Invoke”类型的已垃圾回收委托进行了回调.这可能会导致应用程序崩溃.损坏 ...
 - mybatis-注解实现crud
			
1.在接口上注解sql package com.java1234.mappers; import java.util.List; import org.apache.ibatis.annotation ...
 - vue+node+mongodb实现的页面
			
源代码地址:https://github.com/GainLoss/vue-node-mongodb 目前这个项目实现的是: 1.利用vue-cli实现前台页面的编写 (1)页面的跳转利用的是vue- ...
 - HDU3973 线段树 + 字符哈希
			
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3973 , 线段树 + 字符哈希,好题. 又学了一种新的哈希方法,hhhh~ 解法: 想法是用P进制的数 ...
 - Redis常用特性
			
发布订阅 ·服务器状态在pubsub_channels字典保存了所有频道的订阅关系:SUBSCRIBE命令负责将客户端和被订阅的频道关联到这个字典里面,而UNSUBSCRIBE命令则负责解除客户端和被 ...
 - hdu-1198 Farm Irrigation---并查集+模拟(附测试数据)
			
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1198 题目大意: 有如上图11种土地块,块中的绿色线条为土地块中修好的水渠,现在一片土地由上述的各种 ...
 - Java 集合框架_上
			
集合框架被设计成要满足以下几个目标. 该框架必须是高性能的.基本集合(动态数组,链表,树,哈希表)的实现也必须是高效的. 该框架允许不同类型的集合,以类似的方式工作,具有高度的互操作性. 对一个集合的 ...
 - 使用PinYin4j,获取汉字的拼音字母
			
需要导入的文件 <!-- 引入pinyin4J的依赖 --> <dependency> <groupId>com.belerweb</groupId> ...