当前位置:网站首页>[21 Days Learning Challenge] Image rotation problem (two-dimensional array)
[21 Days Learning Challenge] Image rotation problem (two-dimensional array)
2022-08-04 04:16:00 【Alex holding popcorn】
活动地址:CSDN21天学习挑战赛
怕什么真理无穷,A further joy.
目录
【21天学习挑战赛】图像的旋转问题(二维数组)
Why am I participating in the Challenge
1,机缘
read the first grade,The summer vacation device opened a private message and found that this challenge had the courage to participate.
2,Expected harvest
A, I am studying for a master's degree at South China University of Technology,The current research direction is image restoration,从事深度学习相关工作,The goal is to engageJava后端开发.
B, 期待您的反馈,如果有什么不对的地方,欢迎指正!
C, Looking forward to meeting like-minded peers or technical exchanges.
如果感觉博主的文章还不错的话,还请关注、点赞、收藏三连支持一下博主哦
什么是二维数组?
二维数组是一种结构较为特殊的数组,只是将数组中的每个元素变成了一维数组.所以在JavaThe essence of a two-dimensional array in is still a one-dimensional array,内部的一维数组仍然从索引 0 开始,我们可以将它看作一个矩阵,并处理矩阵的相关问题
Definition of the rotation of an image
Refers to the rotation of an image by a certain angle around a certain point,形成一幅新的图像的过程.Of course this point is usually the center of the image.Since it is rotated according to the center,Naturally there is such a property:The position of the point from the center before and after the rotation does not change.
Steps for image rotation
- 如果是顺时针旋转90°:It must be flipped diagonally first,再水平翻转
- If it is counterclockwise90°:It must be flipped horizontally first,再对角线翻转
Diagonal flip step红色为分割线,按照橙色线进行数据绿色pair-wise exchange(matrix[i][j],matrix[j][i] = matrix[j][i],matrix[i][j])
Flip steps horizontally红色为分割线,按照橙色线进行数据绿色pair-wise exchange(matrix[i][j], matrix[i][length/2- j] = matrix[i][length/2- j] ,matrix[i][j])
️ 算法实现
Because we only need to flip diagonally and horizontally as written
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° * It must be flipped diagonally first,再水平翻转 * If it is counterclockwise90° * It must be flipped horizontally first,再对角线翻转 * * @param matrix */
public void rotate(int[][] matrix) {
//Flip diagonally first
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));
}
}
提交结果
如果觉得对你有帮助的话:
点赞,你的认可是我创作的动力!
️ 收藏,你的青睐是我努力的方向!
️ 评论,你的意见是我进步的财富!
边栏推荐
- centos 安装postgresql13 指定版本
- FFmpeg —— 录制麦克风声音(附源码)
- MySQL query optimization and tuning
- Deep learning -- CNN clothing image classification, for example, discussed how to evaluate neural network model
- 打造一份优雅的简历
- 4-way two-way HDMI integrated business high-definition video optical transceiver 8-way HDMI high-definition video optical transceiver
- 文件系统的简单操作
- mysql index notes
- The general SQL injection flow (sample attached)
- 高效IO模型
猜你喜欢
随机推荐
深度学习——以CNN服装图像分类为例,探讨怎样评价神经网络模型
The video of machine learning to learn [update]
烧录场景下开发如何进行源代码保密工作
嵌入式数据库开发编程MySQL(全)
【C语言进阶】程序环境和预处理
小程序 + 电商,玩转新零售
This Thursday evening at 19:00, the fourth live broadcast of knowledge empowerment丨The realization of equipment control of OpenHarmony smart home project
打造一份优雅的简历
复现20字符短域名绕过
7-1 LVS+NAT load balancing cluster, NAT mode deployment
7. The principle description of LVS load balancing cluster
drools从下载到postman请求成功
Reproduce 20-character short domain name bypass
Enterprise live broadcast is on the rise: Witnessing focused products, micro-like embracing ecology
数据治理平台项目总结和分析
自定义通用分页标签02
Use serve to build a local server
数组相关 内容 解析
7-2 LVS+DR概述与部署
【21天学习挑战赛】直接插入排序









