题目:

You are given an n x n 2D matrix representing an image.

Rotate the image by 90 degrees (clockwise).

思路:

方法一:

可以见矩阵看成多个环组成,如下4*4的矩阵包括两个环,第一个环为1,2,3,4,8,12,16,15,14,13,9,5,1,第二个环为6,7,11,10。

旋转一个矩阵,相当于把每一个环都旋转。如何旋转一个环呢?以最外层的环举例:                      本文地址

旋转前: ,旋转后:

我们把环分成3组:{1,4,16,13},{2,8,15,9},{3,12,14,5},每一组中:旋转后相当于把原来的数字移动到同组中下一个数字的位置

/**
* @param {number[][]} matrix
* @return {void} Do not return anything, modify matrix in-place instead.
*/
var rotate = function(matrix) {
var n=matrix.length;
if(n==0){
return;
} var circle=Math.ceil(n/2),len=n;
for(var i=0;i<circle;i++,len-=2){
var m = len - 1;
for(var j = 0; j < m; j++)
{
var tmp = matrix[i][i+j];
matrix[i][i+j] = matrix[i+m-j][i];
matrix[i+m-j][i] = matrix[i+m][i+m-j];
matrix[i+m][i+m-j] = matrix[i+j][i+m];
matrix[i+j][i+m] = tmp;
}
}
};

方法二:

先将矩阵转置,然后把转置后的矩阵每一行翻转

    转置变为      每一行翻转变为 

【数组】Rotate Image的更多相关文章

  1. 回文数组(Rotate Array (JS))

    旋转一个数组. function rotate(array,n){ var l =array.length,a=array.map(function(x){return x}),arr=[]; n=n ...

  2. [Swift]LeetCode189. 旋转数组 | Rotate Array

    Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: ...

  3. LeetCode 189:旋转数组 Rotate Array

    公众号:爱写bug(ID:icodebugs) 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. Given an array, rotate the array to the ...

  4. LeetCode 189. 旋转数组(Rotate Array)

    189. 旋转数组 LeetCode189. Rotate Array 题目描述 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: [1,2,3,4,5,6, ...

  5. Ruby数组方法整理

    数组方法整理 方法列表: all().any().none()和one():测试数组中的所有或部分元素是否满足给定条件.条件可以是语句块中决定,也可以是参数决定 append():等价于push() ...

  6. Find Minimum in Rotated Sorted Array leetcode java

    题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 ...

  7. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  8. Workflow:采用坐标变换(移动和旋转)画箭头

    背景 流程设计器的连线部分需要画一个箭头代表连接的方向,下图是期望的效果: 刚开始我准备采用三角函数(sin和cos)来计算三角的坐标,实现的过程真不爽(有兴趣的朋友可以试试),就在完工的时候,突然想 ...

  9. [LeetCode] Rotate Array 旋转数组

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

  10. LeetCode——Rotate Image(二维数组顺时针旋转90度)

      问题: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockw ...

随机推荐

  1. post同步请求

    // http://api.hudong.com/iphonexml.do?type=focus-c     //post请求中url不带请求参数,请求参数在参数HTTPBody中设置, 需要创建可变 ...

  2. x11vnc配置--ubuntu14.04

    x11vnc是连接到真实的X会话,相比vnc4server和tightvncserver自己创建不同分辨率的xserver来说,画面延时和显示效果应该要好一些.两种服务都试过,个人感觉x11vnc要好 ...

  3. MySQL性能调优与架构设计——第 15 章 可扩展性设计之Cache与Search的利用

    第 15 章 可扩展性设计之Cache与Search的利用 前言: 前面章节部分所分析的可扩展架构方案,基本上都是围绕在数据库自身来进行的,这样是否会使我们在寻求扩展性之路的思维受到“禁锢”,无法更为 ...

  4. struts2从浅至深(三)拦截器

    一:拦截器概述 Struts2中的很多功能都是由拦截器完成的. 是AOP编程思想的一种应用形式. 二:拦截器执行时机                             interceptor表示 ...

  5. (并查集)Connections in Galaxy War -- zoj --3261 还没写

    链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3261 http://acm.hust.edu.cn/vjudge/ ...

  6. PBOCIC读芯片卡流程

    https://blog.csdn.net/kxd_ysheng/article/details/21178101?_t=t PBOCIC读芯片卡流程,参考上面的博客,整理了一下PBOCIC卡读流程. ...

  7. 深入理解BS结构应用程序

    随着学习的深入,和编程经验的丰富,对BS应用程序有一些认识. 在一些讨论软件技术的QQ群里,或一些社区.BBS中,经常会有一些初学者会犯一些认知性的错误.比如经常会有一些朋友提这样的一些问题:“我怎么 ...

  8. Time&Patience

    “时间和耐心,是世间最强大的两个勇士.” “对未来越有信心,对今天越有耐心,坚持做正确的事.” 每日必做清单 每天六组俯卧撑    活在当下    接收脆弱    一万小时理论(罗马不是一天建成的,胖 ...

  9. Index--过滤索引和参数化

    --============================================ 领导指点我去给某台数据库调优下,结果屁颠屁颠地干完,还自我感觉良好,刚刚别人博客时,才发现自己踩坑了!! ...

  10. BCP 基本语法

    copy from:http://msdn.microsoft.com/zh-cn/library/ms162802.aspx bcp [database_name.] schema.{table_n ...