当前位置:网站首页>2D array theme
2D array theme
2022-07-21 18:23:00 【Pr Young】
1. Power button 48 Rotated image
Rotated image : Rotate the image clockwise 90 degree
step1. First, put this n*n Matrix according to diagonal ( The diagonal from the top left to the bottom right , Instead of the diagonal line from the upper right corner to the lower left corner ) Symmetry . For example, the first row becomes the first column after symmetry
Diagonal symmetric code :
for(int i=0;i<n;i++)
{
for(int j=i;j<n;j++)
{
int temp=nums[i][j];
nums[i][j]=nums[j][i];
nums[j][i]=temp;
}
}
The reason why j from i Start , It means the part with blue line ( The diagonal element of each row and the part to the right of the diagonal element )
step2: Then invert each row of the matrix
For example, the first line is 1,5,7,4, After reversing, it becomes 4,7,5,1
After these two steps, it is found that the matrix has rotated clockwise 90 The degree of
The code implementation is as follows :
class Solution
{
public void rotate(int[][] nums)
{
// This problem requires rotating in place , You cannot use another new matrix to install the rotated matrix
// Find the side length of this square matrix ( Equal length and width )
int n=nums.length;
// First step : First symmetrically along the diagonal
for(int i=0;i<n;i++)
{
for(int j=i;j<n;j++)
{
int temp=nums[i][j];
nums[i][j]=nums[j][i];
nums[j][i]=temp;
}
}
// The second step : Each line is symmetrical
// Take this out matric Every line of the matrix
for(int i=0;i<n;i++)
{
int p=0,q=n-1;
while(p<q)
{
int temp=nums[i][p];
nums[i][p]=nums[i][q];
nums[i][q]=temp;
p++;
q--;
}
}
}
}
2. Power button 54 Spiral matrix
class Solution
{
public List<Integer> spiralOrder(int[][] nums)
{
int m=nums[0].length; // Matrix length
int n=nums.length;// Matrix width
// Define four boundaries
// Note that there m,n Don't write the position of in reverse , It's easy to make a mistake
int left=0; // What column is the left boundary in
int right=m-1; // Which column is the right boundary
int up=0; // Which line is the upper boundary
int down=n-1;// Which line is the lower boundary
List<Integer> result=new ArrayList();
// hold m*n All elements are added to result
while(result.size()<m*n)
{
// It is required to rotate clockwise , So add the upper boundary in turn , Right border , Lower boundary , The number of left bounds
// Add the number of upper boundaries
if(up<=down)
{
for(int temp=left;temp<=right;temp++)
{
result.add(nums[up][temp]);
}
// Move the upper boundary down
up++;
}
// Add the number of right boundaries
if(left<=right)
{
for(int temp=up;temp<=down;temp++)
{
result.add(nums[temp][right]);
}
// Move the right border to the left
right--;
}
// Add the number of lower boundaries
if(up<=down)
{
for(int temp=right;temp>=left;temp--)
{
result.add(nums[down][temp]);
}
// Move lower boundary up
down--;
}
// Add the number of left boundaries
if(left<=right)
{
for(int temp=down;temp>=up;temp--)
{
result.add(nums[temp][left]);
}
// Move left border right
left++;
}
}
return result;
}
}
3.59. Spiral matrix II - Power button (LeetCode)
class Solution
{
public int[][] generateMatrix(int n)
{
int[][] result=new int[n][n];
int up=0;
int down=n-1;
int left=0;
int right=n-1;
// from 1 Start filling the square matrix , Until n The square of
int num=1;
while(num<=n*n)
{
// The square matrix is arranged clockwise
// First fill the upper boundary , Then fill the right boundary , Then fill the lower boundary , Finally, fill the left boundary
// Fill the upper boundary
if(up<=down)
{
for(int temp=left;temp<=right;temp++)
{
result[up][temp]=num;
num++;
}
// Move the upper boundary down
up++;
}
// Fill the right boundary
if(left<=right)
{
for(int temp=up;temp<=down;temp++)
{
result[temp][right]=num;
num++;
}
// Move the right border to the left
right--;
}
// Fill the lower boundary
if(up<=down)
{
for(int temp=right;temp>=left;temp--)
{
result[down][temp]=num;
num++;
}
// Move lower boundary up
down--;
}
// Fill left boundary
if(left<=down)
{
for(int temp=down;temp>=up;temp--)
{
result[temp][left]=num;
num++;
}
// Move left border right
left++;
}
}
return result;
}
}
边栏推荐
- Using zdog to realize JS special effect of outlook lighthouse animation
- 2163. Minimum difference of sum after deleting elements [DP + heapq]
- 不知道 MySQL 咋学?刷完牛客这 50 道题就够了!(第九篇)
- To solve the inherent defects of CNN, ccnn realizes multiple SOTAS with a single architecture
- Clwy permission management (IV) -- menu and permission module
- js发送消息框动画js特效
- Aijs add dotted line
- Zdog floating rotation animation JS special effect code
- 一文读懂 MongoDB 和 MySQL 的差异
- js svg云朵和爱心动画js特效
猜你喜欢
JS SVG cloud and love animation JS special effects
Vulnerability mining -thinkphp6.0.12lts deserialization
Network Security Learning (Qianfeng network security notes) 3 -- batch preparation
[wechat applet] component use and attribute reference
“为了买台手机,研究大半个月后仍然无从选择”
一条 shell 命令的阻塞与唤醒
leetcode:1125. The smallest necessary team [shape pressing DP board + set covering board]
ant.design(简称antd)中时间DatePicker中RangePicker的日期选择限定为只能选择某一天的0点之前的日期
2022-07-20:以下go语言代码是关于json 和 context的,输出什么?A:{};B:{“a“:“b“};C:{“Context“:0};D:不确定。 package main imp
"In order to buy a mobile phone, I still have no choice after half a month of research."
随机推荐
js发送消息框动画js特效
二维数组专题
掌握 Dowanward API 的妙用,轻松拿捏 kubernetes 环境变量
Resume: intelligent cockpit series article 5 - implicit interaction of its three interaction modes
To solve the inherent defects of CNN, ccnn realizes multiple SOTAS with a single architecture
关于swing界面label和button的动态设置文字
Mongodb tutorial Chapter 07 crud search document
Resume: smart cockpit series article 2: the vision of five interactive technologies behind it
【微信小程序】组件使用及属性参考
Niuke-top101-bm34
UneXt 基于MLP的快速医学图像分割网络
Resume: smart cockpit series article 3 - the touch of five interactive technologies behind it
CentOS7安装mysql兼容性问题
win10桌面右下角网络图标中找不到网络
CLWY权限管理(五)--- 用户登录
一. 常见bug——pom文件的project标签爆红
Target detection | target size prediction based on statistical adaptive linear regression
Web3流量聚合平台Starfish OS,给玩家元宇宙新范式体验
CAD浏览模式与绘图模式、CAD如何一次性打印上百张图纸
pytorch-California House Prices