当前位置:网站首页>【21天学习挑战赛】图像的旋转问题(二维数组)
【21天学习挑战赛】图像的旋转问题(二维数组)
2022-08-04 04:07:00 【Alex抱着爆米花】
活动地址:CSDN21天学习挑战赛
怕什么真理无穷,进一步有一份的欢喜。
【21天学习挑战赛】图像的旋转问题(二维数组)
我为什么参与挑战赛
1,机缘
读到研一了,暑假器件打开私信发现这个挑战赛就鼓起勇气参加了。
2,期待的收获
A, 本人在华南理工大学攻读专硕,目前研究方向是图像恢复,从事深度学习相关工作,目标是从事Java后端开发。
B, 期待您的反馈,如果有什么不对的地方,欢迎指正!
C, 期待认识志同道合的领域同行或技术交流。
如果感觉博主的文章还不错的话,还请关注、点赞、收藏三连支持一下博主哦
什么是二维数组?
二维数组是一种结构较为特殊的数组,只是将数组中的每个元素变成了一维数组。所以在Java中二维数组的本质上仍然是一个一维数组,内部的一维数组仍然从索引 0 开始,我们可以将它看作一个矩阵,并处理矩阵的相关问题
图像的旋转的定义
指图像以某一点为中心旋转一定的角度,形成一幅新的图像的过程。当然这个点通常就是图像的中心。既然是按照中心旋转,自然会有这样一个属性:旋转前和旋转后的点离中心的位置不变。
图像的旋转的步骤
- 如果是顺时针旋转90°:一定是先对角线翻转,再水平翻转
- 如果是逆时针旋转90°:一定是先水平翻转,再对角线翻转
斜对角翻转步骤红色
为分割线,按照橙色线
进行数据绿色
的成对交换(matrix[i][j],matrix[j][i] = matrix[j][i],matrix[i][j])
水平翻转步骤红色
为分割线,按照橙色线
进行数据绿色
的成对交换(matrix[i][j], matrix[i][length/2- j] = matrix[i][length/2- j] ,matrix[i][j])
️ 算法实现
因为我们只需要按照写好对角线翻转和水平翻转就可以了
LeetBook题目
旋转矩阵
给你一幅由 N × N 矩阵表示的图像,其中每个像素的大小为 4 字节。请你设计一种算法,将图像旋转 90 度。不占用额外内存空间能否做到?
实例
给定 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]
]
作者:力扣 (LeetCode)
链接:https://leetcode.cn/leetbook/read/array-and-string/clpgd/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
package com.wpc.leetbook.matrix_;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
public class Solution {
/** * 给你一幅由 N × N 矩阵表示的图像,其中每个像素的大小为 4 字节。请你设计一种算法,将图像旋转 90 度。 * 不占用额外内存空间能否做到? * 思路: * 如果是顺时针旋转90° * 一定是先对角线翻转,再水平翻转 * 如果是逆时针旋转90° * 一定是先水平翻转,再对角线翻转 * * @param matrix */
public void rotate(int[][] matrix) {
//先进行斜对角翻转
int length = matrix.length;
int temp = 0;
for (int i = 0; i < length; i++) {
for (int j = i + 1; j < length; j++) {
temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
//在进行水平翻转
int half_length = matrix.length >> 1;
int len = length - 1;
for (int i = 0; i < length; i++) {
for (int j = 0; j < half_length; j++) {
temp = matrix[i][j];
matrix[i][j] = matrix[i][len - j];
matrix[i][len - j] = temp;
}
}
}
@Test
public void test() {
int[][] matrix = new int[][]{
{
5, 1, 9, 11}, {
2, 4, 8, 10}, {
13, 3, 6, 7}, {
15, 14, 12, 16}};
rotate(matrix);
System.out.println(Arrays.deepToString(matrix));
}
}
提交结果
如果觉得对你有帮助的话:
点赞,你的认可是我创作的动力!
️ 收藏,你的青睐是我努力的方向!
️ 评论,你的意见是我进步的财富!
边栏推荐
- This Thursday evening at 19:00, the fourth live broadcast of knowledge empowerment丨The realization of equipment control of OpenHarmony smart home project
- Significant differences between Oracle and Postgresql in PLSQL transaction rollback
- 用户与用户互发红包/支付宝C2C/B2C现金红包php源码示例/H5方式/兼容苹果/安卓
- new Date converts strings into date formats Compatible with IE, how ie8 converts strings into date formats through new Date, how to replace strings in js, and explain the replace() method in detail
- JVM笔记
- How class only static allocation and dynamic allocation
- 拿捏JVM性能优化(自己笔记版本)
- 杭电多校-Slipper-(树图转化+虚点建图)
- MySQL Query Exercise (1)
- SQL注入中 #、 --+、 --%20、 %23是什么意思?
猜你喜欢
七夕节,我用代码制作了表白信封
new Date将字符串转化成日期格式 兼容IE,ie8如何通过new Date将字符串转化成日期格式,js中如何进行字符串替换, replace() 方法详解
基地址:环境变量
Y86. Chapter iv Prometheus giant monitoring system and the actual combat, Prometheus storage (17)
目标检测-中篇
Polygon zkEVM网络节点
【观察】超聚变:首提“算网九阶”评估模型,共建开放繁荣的算力网络
Polygon zkEVM network node
"Introduction to nlp + actual combat: Chapter 8: Using Pytorch to realize handwritten digit recognition"
复现20字符短域名绕过
随机推荐
缓存穿透、缓存击穿、缓存雪崩以及解决方案
MySQL查询优化与调优
JVM的内存模型简介
2022杭电多校联赛第五场 题解
【id类型和NSObject指针 ObjectIve-C中】
How to automatically export or capture abnormal login ip and logs in elastic to the database?
The video of machine learning to learn [update]
Senior PHP development case (1) : use MYSQL statement across the table query cannot export all records of the solution
4-way two-way HDMI integrated business high-definition video optical transceiver 8-way HDMI high-definition video optical transceiver
docker+bridge+redis master-slave+sentry mode
How to open a CITIC Securities online account?is it safe?
PHP高级开发案例(1):使用MYSQL语句跨表查询无法导出全部记录的解决方案
sql语句查询String类型字段小于10的怎么查
系统设计.秒杀系统
一文详解DHCP原理及配置
7-1 LVS+NAT 负载均衡群集,NAT模式部署
Based on the statistical QDirStat Qt directory
如果禁用了安全启动,GNOME 就会发出警告
【Ryerson情感说话/歌唱视听数据集(RAVDESS) 】
Eight guiding principles to help businesses achieve digital transformation success