前几天忘记写题解了…
2022.5.22
每日一题系列_算法提升
https://leetcode.cn/problems/matrix-cells-in-distance-order/
该提示实在有点曲线救国,然而题解那种用sort的方式我看不懂…
class Solution {
public:
static bool cmp(vector& a, vector& b) {
return abs(a[0] - a[2]) + abs(a[1] - a[3]) < abs(b[0] - b[2]) + abs(b[1] - b[3]);
}
vector> allCellsDistOrder(int rows, int cols, int rCenter, int cCenter) {
vector> res;
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
res.push_back({i, j, rCenter, cCenter});
sort(res.begin(), res.end(), cmp);
for (int i = 0; i < res.size(); i++) {
res[i].pop_back();
res[i].pop_back();
}
return res;
}
};



