【leetcode】Rotate Image(middle)
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?
思路:我的思路,先沿对角线对称,再左右对称。

void rotate(int **matrix, int n) {
//先沿对角线对称
for(int i = ; i < n; i++)
{
for(int j = ; j < i; j++)
{
int tmp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = tmp;
}
}
//再左右对称
for(int c = ; c < (n + ) / ; c++)
{
for(int r = ; r < n; r++)
{
int tmp = matrix[r][c];
matrix[r][c] = matrix[r][n - c - ];
matrix[r][n - c - ] = tmp;
}
}
return;
}
大神一步到位的思路:找到每个圈要相互顺时针交换的4个位置,交换。
class Solution {
public:
void rotate(vector<vector<int> > &matrix) {
int n = matrix.size();
if(n<=) return;
for(int i = ; i!=n/;i++) //行 圈数
{
for(int j = i;j!=n--i;j++)
{
swap(matrix[i][j],matrix[j][n--i]);
swap(matrix[n--j][i],matrix[i][j]);
swap(matrix[n--i][n--j],matrix[n--j][i]);
}
}
}
inline void swap(int &a,int &b)
{
a = b+a;
b = a-b;
a = a-b;
}
};
【leetcode】Rotate Image(middle)的更多相关文章
- 【leetcode】Rotate List(middle)
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- 【leetcode】Reverse Integer(middle)☆
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...
- 【leetcode】Reorder List (middle)
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...
- 【leetcode】Word Break (middle)
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- 【leetcode】Partition List(middle)
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- 【leetcode】Spiral Matrix(middle)
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- 【leetcode】Next Permutation(middle)
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 【leetcode】Reverse Bits(middle)
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- 【leetcode】Surrounded Regions(middle)☆
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
随机推荐
- Entity Framework浅析
1.Entity Framework简介 http://www.cnblogs.com/aehyok/p/3315991.html 2.Entity Framework DBFirst尝试http:/ ...
- 大数据之sqoop
一.安装: 1 解压然后把mysql的驱动放在$SQOOP_HOME/lib 目录中2. conf/sqoop-en.sh export HADOOP_COMMON_HOME=/home/hadoop ...
- Mac Pro 安装 Homebrew 软件包管理工具
Linux系统有个让人蛋疼的通病,软件包依赖,好在当前主流的两大发行版本都自带了解决方案,Red hat有 yum,Ubuntu有 apt-get. Mac os 中没有类似的东东,不过有第三方库支持 ...
- cocos2dx中对象的两步初始化
笔者进来开始研究cocos2d这个非常火爆的游戏引擎,在一番折腾后,总算在win7系统下把windows和android平台搭建好了.当然接下来是从官方示例中最简单的HelloCpp项目开始.笔者使用 ...
- 《Zend studio 12 + UPUPW+PHP5.4开发平台配置过程》
一.安装Zend studio 12 安装过程比较简单,就不简述. 二.修改PHP.ini文件 在UPUPW文件夹目录下,找到\upupw\PHP5\php.ini配置文件,并通过搜索 ...
- python——tuple元组
>>> dir(tuple) ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', ...
- Python自动化之sqlalchemy复合外键
复合外键用法 metadata = MetaData(engine) classedu = Table('classedu', metadata, # Column('qq', BigInteger, ...
- python自动化之装饰器
1 高阶函数 满足下列条件之一就可成函数为高阶函数 某一函数当做参数传入另一个函数中 函数的返回值包含n个函数,n>0 高阶函数示范 def bar(): print 'in the bar' ...
- 为什么要继承ActionSupport?
struts2中的action可以继承ActionSupport,也可以不继承ActionSupport.不继承ActionSupport的情况只需要有一个方法,返回String,即可,记住,在继承A ...
- 关于jquery on方法进行事件绑定触发次数指数叠加的问题
发生环境: $modal.on('click', '#search',function(e){}); 上面代码的语法是这样的: .on( events [, selector ] [, data ], ...