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)的更多相关文章

  1. 【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 ...

  2. 【leetcode】Reverse Integer(middle)☆

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...

  3. 【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 ...

  4. 【leetcode】Word Break (middle)

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  5. 【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 ...

  6. 【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 ...

  7. 【leetcode】Next Permutation(middle)

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  8. 【leetcode】Reverse Bits(middle)

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

  9. 【leetcode】Surrounded Regions(middle)☆

    Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...

随机推荐

  1. css3 实现逐帧动画

    css3 实现逐帧动画 实现逐帧动画需要使用到的是Animation动画,该CSS3的Animation有八个属性:分别是如下:1: animation-name2: animation-durati ...

  2. 【翻译】Tomcat 6.0 安装与启动

    本篇来自Tomcat6官方文档:运行手册running.txt 有很多以前都没注意的问题,这里正好学习下. 系列文章来自:<Tomcat官方文档翻译> Tomcat的安装 1 确认本机是否 ...

  3. Nginx安装配置(转)

    Nginx 安装配置 Nginx("engine x")是一款是由俄罗斯的程序设计师Igor Sysoev所开发高性能的 Web和 反向代理 服务器,也是一个 IMAP/POP3/ ...

  4. 2013长沙邀请赛A So Easy!(矩阵快速幂,共轭)

    So Easy! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  5. mysql explain用法和结果的含义

    重点是第二种用法,需要深入的了解. 先看一个例子: mysql> explain select * from t_order; +----+-------------+---------+--- ...

  6. IDEA之maven(springmvc)项目

    1.在idea下创建maven项目(参考IDEA之web项目(maven项目)创建) 2.项目结构 3.web.xml <!DOCTYPE web-app PUBLIC "-//Sun ...

  7. DAY3 python群发短信

    手机轰炸,burpsuit 抓取注册页面输入的手机号,然后每点击一次forword ,都开开始放行,发短信.也可以发到repeat 里面进行 ,重复发送短信. import requests impo ...

  8. linux下用Apache一个IP多个域名建虚拟主机

    如有两个域名,分别是hello.abc.com和play.abc.com,需把这两个域名都绑定到 IP是219.13.34.32的服务器上 1.首先需在域名供应商管理页面指定域名和IP的对应关系 2. ...

  9. OpenCV成长之路(4):图像直方图

    一.图像直方图的概念 图像直方图是反映一个图像像素分布的统计表,其实横坐标代表了图像像素的种类,可以是灰度的,也可以是彩色的.纵坐标代表了每一种颜色值在图像中的像素总数或者占所有像素个数的百分比. 图 ...

  10. String与InputStream互转的几种方法

    [java] view plain copy /** * 利用BufferedReader实现Inputstream转换成String <功能详细描述> * * @param in * @ ...