当前位置:网站首页>[force deduction] 1030. Arrange matrix cells in distance order
[force deduction] 1030. Arrange matrix cells in distance order
2022-07-25 13:44:00 【aigo-2021】
Given four integers row , cols , rCenter and cCenter . There is one rows x cols Matrix , Your coordinates on the cell are (rCenter, cCenter) .
Returns the coordinates of all cells in the matrix , And press and (rCenter, cCenter) Of distance From the smallest to the largest . You can press whatever The answers are returned in the order that this condition is met .
Cell (r1, c1) and (r2, c2) The distance between is |r1 - r2| + |c1 - c2|.
Example 1:
Input :rows = 1, cols = 2, rCenter = 0, cCenter = 0
Output :[[0,0],[0,1]]
explain : from (r0, c0) The distance to other cells is :[0,1]
Example 2:
Input :rows = 2, cols = 2, rCenter = 0, cCenter = 1
Output :[[0,1],[0,0],[1,1],[1,0]]
explain : from (r0, c0) The distance to other cells is :[0,1,1,2]
[[0,1],[1,1],[0,0],[1,0]] It will also be seen as the right answer .
Example 3:
Input :rows = 2, cols = 3, rCenter = 1, cCenter = 2
Output :[[1,2],[0,2],[1,1],[0,1],[1,0],[0,0]]
explain : from (r0, c0) The distance to other cells is :[0,1,1,2,2,3]
Other answers that meet the requirements of the questions will also be considered correct , for example [[1,2],[1,1],[0,2],[1,0],[0,1],[0,0]].
Tips :
1 <= rows, cols <= 100
0 <= rCenter < rows
0 <= cCenter < cols ( This point must be within the range of matrix cells )
class Solution {
public int[][] allCellsDistOrder(int rows, int cols, int rCenter, int cCenter) {
int[][] arr=new int[rows*cols][2];// Define a two-dimensional array that stores the returned results
int index=0;// The subscript of a one-dimensional array in a two-dimensional array
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
arr[index++]=new int[]{i,j};// Traverse to get all the coordinates
}
}
// Sort
Arrays.sort(arr,new Comparator<int[]>(){ // The comparator
@Override
public int compare(int[] r0,int[] r1){ // One dimensional array
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;
}
}
Answer key :
① Define a two-dimensional array arr: The number of one-dimensional arrays in a two-dimensional array is rows*cols, The length of one-dimensional array is 2;
② Through double for Cycle to save all coordinates to a two-dimensional array arr in ;
③ Sort : Use Arrays.sort(T[ ] a, Comparator>? super T<> c) Sort
When calling this method to sort a custom array , We need to specify an external comparator . The second parameter is the custom comparator class , This class must inherit Comparator Interface and Implementation compare Method , So it's right T[] Sort according to the custom comparator rules .
This sort is guaranteed to be stable : Equal elements will not be reordered by the sorting result .
To input :rows = 2, cols = 2, rCenter = 0, cCenter = 1
Output :[[0,1],[0,0],[1,1],[1,0]] For examplePut all the coordinates , Compare the two , Each uses a distance formula |r1 - r2| + |c1 - c2| And target coordinates (rCenter, cCenter) Calculate , Finally, make a difference between the two groups , Return the comparator result , As arr Sort by .
namely : All combinations
Comparing the two [0,1],[0,0] [1,0],[0,1] [1,0],[0,0] [1,1],[0,0] [1,1],[1,0] l1-l2( Distance difference ) -1 2 1 0 -1 Array.sort() Sort two-dimensional arrays
Each row of a two-dimensional array has two elements , How to use two-dimensional array i Arrange the elements of the column ?for example , Right. 0 The elements of the column are arranged in ascending order :
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]));
}Output :
[1, 5]
[2, 4]
[3, 6]
[4, 9]
If to the second 1 The ascending arrangement of column elements only needs return o1[1]-o2[1];

边栏推荐
- Immortal software in the computer that I don't want to delete all my life
- Canal realizes MySQL data synchronization
- 刷题-洛谷-P1152 欢乐的跳
- DNS resolution error during windows unbutu20 lts apt, WGet installation
- Hcip day 6 notes
- Applet H5 get mobile number scheme
- IM系统-消息流化一些常见问题
- Leetcode 113. 路径总和 II
- 命名空间与库
- leetcode202---快乐数
猜你喜欢

Programmer growth chapter 27: how to evaluate requirements priorities?

2022年下半年软考初级程序员备考

QGIS加载在线地图:高德、天地图等

Uncaught SyntaxError: Octal literals are not allowed in strict mode.

Audio and video technology development weekly | 255

刷题-洛谷-P1047 校门外的树

adb通过Wi-Fi连接小米手机

Sports luxury or safety luxury? Which type of Asian Dragon and Volvo S60 should we start with?

Excel record macro

Basic knowledge of binary tree
随机推荐
Error of Tencent cloud [100007] this env is not enable anonymous login
pytest.mark.parametrize及mock使用
刷题-洛谷-P1059 明明的随机数
ADB connects to Xiaomi mobile phone via Wi Fi
Applet H5 get mobile number scheme
Hcip seventh day notes
0720RHCSA
HTTP cache tongtianpian, there may be something you want
Canal realizes MySQL data synchronization
剑指offer专项突击版第10天
【力扣】1030.距离顺序排列矩阵单元格
Explain the precision of floating point numbers in detail
Okaleido launched the fusion mining mode, which is the only way for Oka to verify the current output
mysql 01: source命令
@Classmethod decorator
From input URL to web page display
Blindly expanding the scale of the meta universe has deviated from the development logic of the meta universe
0716RHCSA
leetcode202---快乐数
Business visualization - make your flowchart'run'(3. Branch selection & cross language distributed operation node)