当前位置:网站首页>【力扣】1030.距离顺序排列矩阵单元格
【力扣】1030.距离顺序排列矩阵单元格
2022-07-25 13:38:00 【aigo-2021】
给定四个整数 row , cols , rCenter 和 cCenter 。有一个 rows x cols 的矩阵,你在单元格上的坐标是 (rCenter, cCenter) 。
返回矩阵中的所有单元格的坐标,并按与 (rCenter, cCenter) 的 距离 从最小到最大的顺序排。你可以按 任何 满足此条件的顺序返回答案。
单元格(r1, c1) 和 (r2, c2) 之间的距离为|r1 - r2| + |c1 - c2|。
示例 1:
输入:rows = 1, cols = 2, rCenter = 0, cCenter = 0
输出:[[0,0],[0,1]]
解释:从 (r0, c0) 到其他单元格的距离为:[0,1]
示例 2:
输入:rows = 2, cols = 2, rCenter = 0, cCenter = 1
输出:[[0,1],[0,0],[1,1],[1,0]]
解释:从 (r0, c0) 到其他单元格的距离为:[0,1,1,2]
[[0,1],[1,1],[0,0],[1,0]] 也会被视作正确答案。
示例 3:
输入:rows = 2, cols = 3, rCenter = 1, cCenter = 2
输出:[[1,2],[0,2],[1,1],[0,1],[1,0],[0,0]]
解释:从 (r0, c0) 到其他单元格的距离为:[0,1,1,2,2,3]
其他满足题目要求的答案也会被视为正确,例如 [[1,2],[1,1],[0,2],[1,0],[0,1],[0,0]]。
提示:
1 <= rows, cols <= 100
0 <= rCenter < rows
0 <= cCenter < cols (该点一定是在矩阵单元格范围之内的)
class Solution {
public int[][] allCellsDistOrder(int rows, int cols, int rCenter, int cCenter) {
int[][] arr=new int[rows*cols][2];//定义存储返回结果的二维数组
int index=0;//二维数组中一维数组的下标
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
arr[index++]=new int[]{i,j};//遍历得到所有的坐标
}
}
//排序
Arrays.sort(arr,new Comparator<int[]>(){ //比较器
@Override
public int compare(int[] r0,int[] r1){ //一维数组
int l1=Math.abs(r0[0]-rCenter)+Math.abs(r0[1]-cCenter);
int l2=Math.abs(r1[0]-rCenter)+Math.abs(r1[1]-cCenter);
return l1-l2;
}
});
return arr;
}
}
题解:
①定义二维数组arr:二维数组中一维数组的个数是rows*cols,一维数组长度是2;
②通过双重for循环将所有坐标存到二维数组arr中;
③排序:使用Arrays.sort(T[ ] a, Comparator>? super T<> c)进行排序
当调用此方法来进行自定义数组排序时,需要我们指定外部比较器。第二个参数就是自定义的比较器类,这个类必须继承Comparator接口并实现compare方法,于是就对T[]按照自定义的比较器规则进行排序。
此排序被保证是稳定的:相等的元素不会被重新排序的排序结果。
以输入:rows = 2, cols = 2, rCenter = 0, cCenter = 1
输出:[[0,1],[0,0],[1,1],[1,0]]为例将所有的坐标,两两进行比较,每个都用距离公式|r1 - r2| + |c1 - c2|与目标坐标(rCenter, cCenter)进行计算,最后将两组得到的距离做差,将该比较器结果返回,作为arr排序的依据。
即:所有的组合
两两比较 [0,1],[0,0] [1,0],[0,1] [1,0],[0,0] [1,1],[0,0] [1,1],[1,0] l1-l2(距离做差) -1 2 1 0 -1 Array.sort()对二维数组进行排序
二维数组每一行有两个元素,如何对二维数组利用第i列的元素进行排列?例如,对第0列的元素进行升序排列:
int[][] temp=new int[][]{ {3,6},{2,4},{1,5},{4,9}};
Arrays.sort(temp, new Comparator<int[]>(){@Override
public int compare(int[] o1, int[] o2) {
return o1[0]-o2[0];
}});
for(int i=0;i<temp.length;i++){
System.out.println(Arrays.toString(temp[i]));
}输出:
[1, 5]
[2, 4]
[3, 6]
[4, 9]
如果对第1列元素升序排列只需要return o1[1]-o2[1];

边栏推荐
- The simplest solution of the whole network 1045 access denied for user [email protected] (using password:YES)
- QGIS loading online map: Gaode, Tiandi map, etc
- 2022年下半年软考信息安全工程师如何备考?
- 基于百问网IMX6ULL_PRO开发板驱动AP3216实验
- Install mujoco and report an error: distutils.errors DistutilsExecError: command ‘gcc‘ failed with exit status 1
- 刷题-洛谷-P1161 开灯
- 6.27 uniapp project history
- hcip第六天笔记
- Based on Baiwen imx6ull_ Pro development board transplants LCD multi touch driver (gt911)
- ES6数组去重 new Set()
猜你喜欢

刷题-洛谷-P1059 明明的随机数

IM系统-消息流化一些常见问题

Programmer growth chapter 27: how to evaluate requirements priorities?
![[server data recovery] HP EVA server storage raid information power loss data recovery](/img/ee/8f36ef1b5842f1778c0dd695401b40.jpg)
[server data recovery] HP EVA server storage raid information power loss data recovery
[email protected] (using password:YES)"/>The simplest solution of the whole network 1045 access denied for user [email protected] (using password:YES)

0710RHCSA

Canal realizes MySQL data synchronization
[email protected](using password:YES)"/>全网最简单解决方式1045-Access denied for user [email protected](using password:YES)

ES6 array de duplication new set()

Redis visualizer RDM installation package sharing
随机推荐
0715RHCSA
0717RHCSA
The whole process of 6w+ word recording experiment | explore the economical data storage strategy of alluxio
Excel add key run macro
IM系统-消息流化一些常见问题
0716RHCSA
How to realize the configuration method of user password free login?
Excel record macro
0716RHCSA
Brpc source code analysis (III) -- the mechanism of requesting other servers and writing data to sockets
互斥锁、自旋锁、读写锁……理清它们的区别和应用
C#基础学习(二十三)_窗体与事件
Introduction to jupyter notebook
0713RHCSA
0710RHCSA
Congestion control of TCP
【GCN-CTR】DC-GNN: Decoupled GNN for Improving and Accelerating Large-Scale E-commerce Retrieval WWW22
刷题-洛谷-P1046 陶陶摘苹果
Blocking queue for concurrent programming
Leetcode 113. path sum II