0%

[題解]Leetcode 1030 Matrix Cells in Distance Order

1030 Matrix Cells in Distance Order

題目連結

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public:
vector<vector<int>> allCellsDistOrder(int rows, int cols, int rCenter, int cCenter) {
int n = rows,m = cols;
multimap<int,pair<int,int>> mp;
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
int dis = abs(i - rCenter) + abs(j - cCenter);
mp.insert({dis,{i,j}});
}
}
vector<vector<int>> ans;ans.resize(n*m);
for(int i=0;i<n*m;i++)ans[i].resize(2,0);
int id = 0;
for(auto it : mp){
ans[id][0] = (it.second.first);
ans[id][1] = (it.second.second);
id++;
}
return ans;
}
};