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. 黄学长模拟day1 大逃亡

    给出数字N(1<=N<=10000),X(1<=x<=1000),Y(1<=Y<=1000),代表有N个敌人分布一个X行Y列的矩阵上,矩形的行号从0到X-1,列号从 ...

  2. jquery datagrid加载后仅选定第一行

    function onLoadSuccess(data) { var rows = $("#DataGrid").datagrid("getRows"); if ...

  3. ZOJ 3711 Give Me Your Hand

    Give Me Your Hand Time Limit: 2 Seconds      Memory Limit: 131072 KB      Special Judge BellyWhite a ...

  4. PHP array_multisort() 函数详解 及 二维数组排序(模拟数据表记录按字段排序)

    一.先看最简单的情况. 有两个数组: $arr1 = array(1, 9, 5); $arr2 = array(6, 2, 4); array_multisort($arr1, $arr2); pr ...

  5. linux 驱动程序中断号的申请

    1.linux下查看硬中断与软中断 硬中断:/proc/interrupts 软中断:/proc/softirqs 往往一些硬件中断号都是跟CPU所分配的硬件中断号相匹配的. 即同类型的cpu可能对同 ...

  6. 霸气!Nginx 中缓存静态文件秘籍

    导读 这篇教程说明你应该怎样配置 nginx.设置 HTTP 头部过期时间,用 Cache-Control 中的 max-age 标记为静态文件(比如图片. CSS 和 Javascript 文件)设 ...

  7. echarts

    ECharts,缩写来自Enterprise Charts,商业级数据图表,一个纯Javascript的图表库,可以流畅的运行在PC和移动设备上,兼容当前绝大部分浏览器(IE6/7/8/9/10/11 ...

  8. zend framework2 入门实例代码album模型

    下载album模型 一.目录结构说明 - zf_project - config    - autoload      global.php    -- 数据库在这里配置      local.php ...

  9. 1.5---字符串压缩(CC150)

    import java.util.*; public class Zipper { public String zipString(String str) { // write code here i ...

  10. discuzX3后台管理插件开发示例一 用户表查询

    上次的入门已经介绍了后台管理插件开发的基本步骤,下面简单写一个示例查询一下用户表 需要已完成以下操作: 1.已创建test后台管理插件 //详见 http://www.cnblogs.com/savo ...