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. C#创建windows服务列表

    转载自:http://www.cnblogs.com/sorex/archive/2012/05/16/2502001.html Windows Service这一块并不复杂,但是注意事项太多了,网上 ...

  2. Tomcat 部署详解

    前篇大概讲过了tomcat的发布和部署,以及测试. 本篇详细介绍下tomcat的部署相关的内容,本篇主要内容: 1 介绍下部署相关的内容,需要安装的文件以及上下文描述符 2 静态部署——在tomcat ...

  3. hdu.1010.Tempter of the Bone(dfs+奇偶剪枝)

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  4. 机器学习之Hash集合问题

    问题来源与七月学习之 (3.x线性代数与矩阵运算基础)

  5. MySQL也有潜规则 – Select 语句不加 Order By 如何排序?

    今天遇到一个问题,有一个 Select 语句没有加 "Order By",返回的数据是不确定的. 这种问题碰到不止几次了.追根寻底, Select 语句如果不加 "Ord ...

  6. 使用APPCAN开发移动应用APP心得

    要想使用APPCAN开发移动应用,首先要弄明白什么是APPCAN,APPCAN都具有哪些功能. 1.什么是APPCAN? APPCAN是正益无线公司开发的一套Hybrid混合应用开发平台(AppCan ...

  7. Mysql BLOB和TEXT类型

      BLOB是一个二进制大对象,可以容纳可变数量的数据.有4种BLOB类型:TINYBLOB.BLOB.MEDIUMBLOB和LONGBLOB.它们只是可容纳值的最大长度不同. A binary la ...

  8. linux 使用rpm安装软件时,遇到"warning: rpmts_HdrFromFdno: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY "错误

    建议的做法: warning: rpmts_HdrFromFdno: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY    网上资料说这是 ...

  9. jQuery中的checkbox问题

    一开始的代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  10. 贴一下WC总结里提到的那道裸题吧。。。

    [bzoj4034][HAOI2015]T2 试题描述 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 x 的点权增加 a . 操作 2 ...