【Leetcode】48. 旋转图像(Rotate Image)

LeetCode 】: 48. 旋转图像Rotate Image LeetCode48 - 旋转图像Rotate Image) 问题描述: 给定一个 n × n 的二维矩阵,表示一个图像。将图像顺时针旋转 90 度。 说明 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要使用另一个矩阵来旋转图像。 示例1 给定 matrix = [ [1,2,3], [4,5,6], [7,8,9] ], 原地旋转输入矩阵,使其变为: [ [7,4... 阅读详情

Leetcode - 48 Rotate Image (Medium)

题目描述:将一个 n × n 的矩阵旋转 90°,不能使用额外的辅助空间。

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]
]

解题思路:原地旋转,这个时候就需要找规律了,规律:先左右交换,在以对称轴(斜上)交换。

public void rotate(int[][] matrix) {
    for(int i = 0; i<matrix.length; i++){
        for(int j = i; j<matrix[0].length; j++){
            int temp = 0;
            temp = matrix[i][j];
            matrix[i][j] = matrix[j][i];
            matrix[j][i] = temp;
        }
    }
    for(int i =0 ; i<matrix.length; i++){
        for(int j = 0; j<matrix.length/2; j++){
            int temp = 0;
            temp = matrix[i][j];
            matrix[i][j] = matrix[i][matrix.length-1-j];
            matrix[i][matrix.length-1-j] = temp;
        }
    }
}
LeetCode旋转图像 系列文章目录 文章目录系列文章目录前言一、旋转图像二、使用步骤解法一:先延反对角线对称,再上下翻转解法二:总结 前言 一、旋转图像 给定一个 n × n 的二维矩阵 matrix 表示一个图像。请你将图像顺时针旋转 90 度。 你必须在 原地 旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要 使用另一个矩阵来旋转图像。 示例 1: 输入:matrix = [[1,2,3],[4,5,6],[7,8,9]] 输出:[[7,4,1],[8,5,2],[9,6,3]] 示例 2: 输入 阅读详情

相关推荐

Leet-code 旋转图像

一、题目概述: 【1】题目原型: 【2】原题连结: 48. 旋转图像 - 力扣(LeetCode(leetcode-cn.com) 二、解题思路: 旋转问题我们不妨先只看部分,我们明显可以观察到:每次旋转时只和四个数有关,我们不妨先通过双重for循环以每边中点为界将其分为4份。 for(int row = 0,row < n/2,row ++){ for(int line = 0,line < n/2,line ++){ } } 然后

qq_62678419的博客 166

LeetCode-48. 旋转图像数组 数学 矩阵】

输入:matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]输出:[[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]你必须在 原地 旋转图像,这意味着你需要直接修改输入的二维矩阵。给定一个 n × n 的二维矩阵 matrix 表示一个图像。输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]输出:[[7,4,1],[8,5,2],[9,6,3]]空间复杂度:O(1)

zik的博客 1497

LeetCode 面试经典150题】48. Rotate Image 旋转图像

n x n。

qq_43513394的博客 582

leetcode48——旋转图像

旋转图像 题目描述:给定一个 n × n 的二维矩阵 matrix 表示一个图像。请你将图像顺时针旋转 90 度 要求:你必须在 原地 旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要 使用另一个矩阵来旋转图像。 辅助矩阵 我们通过分析示例可以看出,转换后的矩阵和转换前的矩阵有如下关系 我们可以得出对于矩阵中第i行j列的元素,在旋转后出现在倒数第i列第j行的位置,我们可以得到如下关系。 matrix_new[j][n - i - 1] = matrix[i][j]; 所以我们可以使用一个

weixin_46107667的博客 908

Leetcode 48. Rotate Image 旋转图像

Leetcode 48. Rotate Image 旋转图像 标签: Leetcode 题目地址:https://leetcode-cn.com/problems/rotate-image/ 题目描述 给定一个 n × n 的二维矩阵表示一个图像。 将图像顺时针旋转 90 度。 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要使用另一个矩阵来旋转图像...

CoderWangSon 274

leetcode 48. Rotate Image-旋转图像|python

旋转图像,这是一个在leetcoed上十分有意思的例子,也具有一定的实用性,尤其在图像处理领域应用较多,在leetcode上也比较受欢迎。在这一篇分享当中,会在解答本题的顺便分享一下解题过程当中的几个知识点。首先看看题目描述:给定一个 n × n 的二维矩阵表示一个图像。将图像顺时针旋转 90 度。说明:你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要使用另一个矩阵来旋转图像。示...

m0_37985723的博客 887

LeetCode典型题目系列 48. Rotate Image 旋转图像

LeetCode典型题目系列 48. Rotate ImageLeetCode典型题目系列题目 48. Rotate Image思路编程技巧Code (Java) LeetCode典型题目系列 作为一名学计算机的学生,之前一直少有练习OJ,写博客,维护自己的GitHub的觉悟,说到底还是太懒了。春招以来愈发觉得练习OJ的重要性,于是准备写一些博客,记录自己遇到的典型题目和思考,一方面是给自己的笔记...

是阿福呀 594

Leetcode48.Rotate Image旋转图像.java

题目 给定一个 n × n 的二维矩阵表示一个图像。 将图像顺时针旋转 90 度。 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要使用另一个矩阵来旋转图像。 示例 1: 给定 matrix = [ [1,2,3], [4,5,6], [7,8,9] ], 原地旋转输入矩阵,使其变为: [ [7,4,1], [8,5,2], [9,6,3] ] 示例 2: 给定 matrix = [ [ 5, 1, 9,11], [ 2, 4, 8,10], [13, 3, 6, 7], [

梦晨涌京 287

LeetCode 48 Rotate Image旋转图像

翻译给定一个n∗nn * n的2D矩阵表示一个图像。顺时针旋转90度。跟进: 你可以就地完成它吗?原文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?分析尊重原创,一个很好的

nomasp 7586

Leetcode 48.旋转图像Rotate Image

Leetcode 48.旋转图像 1 题目描述(Leetcode题目链接)   给定一个 n × n 的二维矩阵表示一个图像。将图像顺时针旋转 90 度。   说明:你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要使用另一个矩阵来旋转图像。 示例: 给定 matrix = [ [1,2,3], [4,5,6], [7,8,9] ], 原地旋转输入矩阵,使其变为: ...

qq_39378221的博客 268

LeetCode48题思悟——旋转图像rotate-image

LeetCode48题思悟——旋转图像rotate-image) 知识点预告 坐标规律计算——数学知识; 正方形中对中点的计算; 题目要求 给定一个 n × n 的二维矩阵表示一个图像。 将图像顺时针旋转 90 度。 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要使用另一个矩阵来旋转图像。 来源:力扣(LeetCode) 链接:https://leetcode...

博采众长,自成一派; 403

LeetCode | 48. 旋转图像 (Rotate Image)

链接:https://leetcode.cn/problems/rotate-image/solutions/1228078/48-xuan-zhuan-tu-xiang-fu-zhu-ju-zhen-yu-jobi/ 来源:力扣(LeetCode) 著作权归作者所有。输入:matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]输出:[[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]

Lynn0_Y的博客 456

Leetcode48. Rotate Image旋转图像

给定一个n×n的二维矩阵表示一个图像。 将图像顺时针旋转 90 度。 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要使用另一个矩阵来旋转图像。 示例 1: 给定 matrix = [ [1,2,3], [4,5,6], [7,8,9] ], 原地旋转输入矩阵,使其变为: [ [7,4,1], [8,5,2],...

Ha12312的博客 134

[leetcode]48. Rotate Image旋转图像

You are given annxn2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Note: You have to rotate the imagein-place, which means you have to modify the input 2D matrix d...

weixin_30376509的博客 87

LeetCode】#48旋转图像(Rotate Image)

LeetCode】#48旋转图像(Rotate Image) 题目描述 给定一个 n × n 的二维矩阵表示一个图像。 将图像顺时针旋转 90 度。 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要使用另一个矩阵来旋转图像。 示例 示例 1: 给定 matrix = [ [1,2,3], [4,5,6], [7,8,9] ], 原地旋转输入矩阵,使其变为: [ [7,4...

weixin_43858604的博客 155

LeetCode 48. 旋转图像 Rotate Image

给定一个 n × n 的二维矩阵表示一个图像。 将图像顺时针旋转 90 度。 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要使用另一个矩阵来旋转图像。 示例 1: 给定 matrix = [ [1,2,3], [4,5,6], [7,8,9] ], 原地旋转输入矩阵,使其变为: [ [7,4,1], [8,5,2], [9,6,...

Umbrella Corporation 1万+

leetcode-48. Rotate Image 旋转图像,矩阵旋转90度

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 inpu...

别说话写代码的博客 883

旋转图像Rotate Image)- LeetCode 48

这是一个经典的二维矩阵操作题。理解转置和水平翻转的基本概念,以及正确的执行顺序,将帮助我们高效地解决这个问题。通过这篇文章,希望你能够更深入地理解如何在Java中实现这一算法。

weixin_40185596的博客 765
上一篇: 【Leetcode】46. 全排列(Permutations)
下一篇: 【Leetcode】41. 缺失的第一个正整数(First Missing Positive)
str_818
博客等级 码龄11年 118粉丝 215原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值