当前位置:网站首页>[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));
}
}
提交结果
如果觉得对你有帮助的话:
点赞,你的认可是我创作的动力!
️ 收藏,你的青睐是我努力的方向!
️ 评论,你的意见是我进步的财富!
边栏推荐
猜你喜欢
复现20字符短域名绕过
Oracle与Postgresql在PLSQL内事务回滚的重大差异
软件测试如何系统规划学习呢?
Polygon zkEVM network node
2 Gigabit Optical + 6 Gigabit Electric Rail Type Managed Industrial Ethernet Switch Supports X-Ring Redundant Ring One-key Ring Switch
机器学习之视频学习【更新】
Explain detailed explanation and practice
if,case,for,while
mq应用场景介绍
4-way two-way HDMI integrated business high-definition video optical transceiver 8-way HDMI high-definition video optical transceiver
随机推荐
Introduction to mq application scenarios
Postgresql源码(66)insert on conflict语法介绍与内核执行流程解析
Metaverse "Drummer" Unity: Crazy expansion, suspense still exists
张量篇-应用案例
How to automatically export or capture abnormal login ip and logs in elastic to the database?
帮助企业实现数字化转型成功的八项指导原则
Mockito unit testing
【21天学习挑战赛】顺序查找
SVM介绍以及实战
【id类型和NSObject指针 ObjectIve-C中】
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
Senior PHP development case (1) : use MYSQL statement across the table query cannot export all records of the solution
How to open a CITIC Securities online account?is it safe?
y86.第四章 Prometheus大厂监控体系及实战 -- prometheus存储(十七)
基本表单验证流程
【MD5】采用MD5+盐的加密方式完成注册用户和登录账号
Functions, recursion and simple dom operations
小程序 + 电商,玩转新零售
【21天学习挑战赛】图像的旋转问题(二维数组)
类如何只能静态分配和只能动态分配