Difficulty:medium

 More:【目录】LeetCode Java实现

Description

https://leetcode.com/problems/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 NOTallocate 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]
]

Example 2:

Given input matrix =
[
[ 5, 1, 9,11],
[ 2, 4, 8,10],
[13, 3, 6, 7],
[15,14,12,16]
], rotate the input matrix in-place such that it becomes:
[
[15,13, 2, 5],
[14, 3, 4, 1],
[12, 6, 8, 9],
[16, 7,10,11]
]

Intuition

  1. transpose the matrix

  2. flip the matrix horizontally

  1 2 3   1 4 7   7 4 1
  4 5 6  =>  2 5 8  =>  8 5 2
  7 8 9   3 6 9   9 6 3

Solution

    public void rotate(int[][] a) {
int n = a[0].length;
//transpose the matrix
for(int i=0; i<n; i++){
for(int j=i+1; j<n; j++){
int temp = a[i][j];
a[i][j]=a[j][i];
a[j][i]=temp;
}
}
//flip the matrix horizontally
for(int i=0; i<n; i++){
for(int j=0; j<n/2; j++){
int temp = a[i][j];
a[i][j] = a[i][n-1-j];
a[i][n-1-j] = temp;
}
}
}

  

Complexity

Time complexity : O(N)

Space complexity : O(1)

What I've learned

1. When rotating a matrix, we can transpose the matrix firstly, and then find the law.

 More:【目录】LeetCode Java实现

【LeetCode】48. Rotate Image的更多相关文章

  1. 【LeetCode】48. Rotate Image 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  2. 【LeetCode】48. Rotate Image (2 solutions)

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

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

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

  4. 【一天一道LeetCode】#48. Rotate Image

    一天一道LeetCode系列 (一)题目 You are given an n x n 2D matrix representing an image. Rotate the image by 90 ...

  5. 【LeetCode】189. Rotate Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 切片 递归 日期 题目地址:https://leet ...

  6. 【LeetCode】189 - Rotate Array

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

  7. 【leetcode】61. Rotate List

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

  8. 【LeetCode】048. Rotate Image

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

  9. 【LEETCODE】48、数组分类,简单级别,题目:189,217,219,268,283,414

    package y2019.Algorithm.array; import java.util.Arrays; import java.util.Stack; /** * @ClassName Rot ...

随机推荐

  1. 动态加载swiper,默认显示最后一个swiper-slide解决方案???

    问题描述: 用ajax动态加载swiper-slide以后,由于我是自适应屏幕的尺寸来决定一屏显示多少图片,所以加了 slidesPerView:'auto'这条属性,加了这条属性过后,每次刷新页面的 ...

  2. 从架构开始谈dubbo(二)-----zookeeper安装和dubbo文档查阅

    一.Zookeeper下载与安装 1.官网地址下载 https://zookeeper.apache.org/ 2.找到图中英文位置(英文不好,安图操作就好了,计算机很多东西如果你没法很好的记忆,就画 ...

  3. 浏览器bug html 底部

  4. IComparable和IComparer接口

    C#中,自定义类型,支持比较和排序,需要实现IComparable接口.IComparable接口存在一个名为CompareTo()的方法,接收类型为object的参数表示被比较对象,返回整型值:1表 ...

  5. VS 编译总是出现错误: "LC.EXE 已退出,代码为-1"

    最近在开发CS的一个项目时,编译总是出现错误: "LC.EXE 已退出,代码为-1" 解决方法一:用记事本打开*.licx,里面写的全是第三方插件的指定DLL,删除错误信息,保存, ...

  6. Scrapy笔记08- 文件与图片

    Scrapy笔记08- 文件与图片 Scrapy为我们提供了可重用的item pipelines为某个特定的Item去下载文件. 通常来说你会选择使用Files Pipeline或Images Pip ...

  7. Pandas | 11 字符串函数

    在本章中,我们将使用基本系列/索引来讨论字符串操作.在随后的章节中,将学习如何将这些字符串函数应用于数据帧(DataFrame). Pandas提供了一组字符串函数,可以方便地对字符串数据进行操作. ...

  8. 创建、查看、删除计划任务at命令举例

    1.三天后的下午 5 点执行 /bin/ls : at 5pm + 3 days at> /bin/ls             结束按ctrl+d 查看计划任务:at -l 之后 at -c ...

  9. 从Oop-Klass模型看透反射

    <红楼梦>第十二回,贾瑞因痴迷王熙凤,被王熙凤折腾的眼看就快不行了.当然这里面是没有多少爱的,完全因王熙凤的美貌而起.就在这时来了一个跛足道人,带来了一面宝镜,说能治好贾瑞的病.当然这可不 ...

  10. 剑指offer 6:链表(从头到尾打印链表)

    链表的数据结构 struct ListNode { int value; ListNode* next; }; 那么在链表的末尾添加一个节点的代码如下: void insert(ListNode** ...