48. Rotate Image

leetcode 48. Rotate Image

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

Rotate the image by 90 degrees (clockwise).

Note:
You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.

Example 1:

Given input matrix =
[
[1,2,3],
[4,5,6],
[7,8,9]
],

rotate the input matrix in-place such that it becomes:
[
[7,4,1],
[8,5,2],
[9,6,3]
]

思路:

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

var rotate = function(matrix) {
    for(var i=0;i<matrix.length;i++){
        for(j=i+1;j<matrix.length;j++){
            var temp=matrix[i][j];
            matrix[i][j]=matrix[j][i];
            matrix[j][i]=temp;
        }
        matrix[i].reverse();
       
    }
};
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 14,357评论 0 33
  • 阿菱:书山有路勤为径,学海无涯苦作舟。这是学校大门上的一幅对联。上学真的是这么难,这么苦吗? 阿球:这种说法,让人...
    云水坡头阅读 3,280评论 0 0
  • 提起济南,想起老舍,:一个老城,有山有水,全在天底下晒着阳光,暖和安适的睡着,只等春风来把它们唤醒,这是不是个理想...
    wheat_麦阅读 2,523评论 0 0
  • 七年 婚姻中有一个词,叫做“七年之痒”,大致意思是一对夫妻如果结婚了后能够一起过日子七年,也就彻底的熟悉并且...
    孤独别硬来阅读 5,312评论 0 0

友情链接更多精彩内容