Rotate Image

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?

 
 
假设旋转的时候,左上角元素为(l,l),右下角元素为(r,r)
则旋转的时候
(l,l+1)=(r-1,l) 最上面一排 
(r-1,l)=(r,r-1)左边一排
(r,r-1)=(l+1,r)下面一排
(l+1,r)=(l,l+1)右边一排
 
可以找到规律
(i,j)--->(r+l-j,i)
 

 class Solution {
public:
void rotate(vector<vector<int> > &matrix) { int n=matrix.size();
int leftUp=;
int rightBottom=n-;
while(rightBottom>leftUp)
{
int n0=rightBottom+leftUp;
for(int i=leftUp;i<rightBottom;i++)
{
int tmp=matrix[leftUp][i];
matrix[leftUp][i]=matrix[n0-i][leftUp];
matrix[n0-i][leftUp]=matrix[n0-leftUp][n0-i];
matrix[n0-leftUp][n0-i]=matrix[i][n0-leftUp];
matrix[i][n0-leftUp]=tmp; } leftUp++;rightBottom--;
}
}
};
 
 

【leetcode】Rotate Image的更多相关文章

  1. 【LeetCode】 Rotate List 循环链表

    题目:rotate list 解法1: <span style="font-size:18px;">/**LeetCode Rotate List:Given a li ...

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

  3. 【leetcode】Rotate Image(middle)

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  4. 【LeetCode】Rotate Array

    Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = ...

  5. 【题解】【矩阵】【回溯】【Leetcode】Rotate Image

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  6. 【LeetCode】Rotate List

    Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...

  7. 【LeetCode】61. Rotate List 解题报告(Python)

    [LeetCode]61. Rotate List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...

  8. 【LeetCode】数学(共106题)

    [2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...

  9. 【LeetCode】双指针 two_pointers(共47题)

    [3]Longest Substring Without Repeating Characters [11]Container With Most Water [15]3Sum (2019年2月26日 ...

随机推荐

  1. git merge 和 git rebase 小结

    Git merge是用来合并两个分支的. git merge b # 将b分支合并到当前分支 同样 git rebase b,也是把 b分支合并到当前分支 ---------------------- ...

  2. Node对象属性

    1.Node对象属性一            * nodeName             * nodeType            * nodeValue * 使用dom解析html时候,需要ht ...

  3. phpcms使用细节

    1.在模板中使用php语句 <?php for ($i=0; $i < 10; $i++) {     echo $i."#######<br>"; }?& ...

  4. Task异常处理

    http://www.cnblogs.com/xray2005/archive/2011/08/24/2151459.html

  5. MySQL各版本的区别

    文章出自:http://blog.sina.com.cn/s/blog_62b37bfe0101he5t.html 感谢作者的分享 MySQL 的官网下载地址:http://www.mysql.com ...

  6. strtol,strtoll,strtoul, strtoull字符串转化成数字

      今天看kafka,有一个参数选项中有: 'S'   seq=strtoull(optarg,NULL,10); do_seq=1; 之后查找了下 strtoull 函数的功能,了解如下: ---- ...

  7. Subversion命令汇总

    转自:http://www.cnblogs.com/cnblogsfans/archive/2010/03/21/1690838.html svn 命令共同的选项 --targets list 读取l ...

  8. POJ 1947 Rebuilding Roads

    树形DP..... Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8188 Accepted: ...

  9. tc 146 2 BridgeCrossing(n人过桥问题)

    SRM 146 2 1000BridgeCrossing Problem Statement A well-known riddle goes like this: Four people are c ...

  10. Python命令 (if __name__=="__main__":)

    1. 语法 1.以#号开头的语句是注释 2.请务必注意,Python程序是大小写敏感的,如果写错了大小写,程序会报错. 3.按照约定俗成的管理,应该始终坚持使用4个空格的缩进. 4.当语句以冒号:结尾 ...