Python解法:
class Solution: def largestPerimeter(self, A: List[int]) -> int: A.sort() for i in range(len(A) - 3, -1, -1): if A[i] + A[i+1] > A[i+2]: return A[i] + A[i+1] + A[i+2] return 0
C++解法:
#include <iostream> #include <vector> #include <map> #include <algorithm> using namespace std; class Solution { public: vector<vector<int>> allCellsDistOrder(int R, int C, int r0, int c0) { vector<vector<int>> res(R*C, vector<int>(3));//数组第三个位置存放两点的曼哈顿距离 int num = 0; for (int i = 0; i<C; i++) { for (int j = 0; j<R; j++) { res[num][0] = j; res[num][1] = i; res[num][2] = abs(r0 - j) + abs(c0 - i);//曼哈顿距离 num++; } } sort(res.begin(), res.end(), ismax);//排序 for (int i = 0; i<num; i++)//将曼哈顿距离删除 { res[i].pop_back(); } return res; } static bool ismax(vector<int> &a, vector<int> &b)//根据曼哈顿距离升序排序 { return a[2]<b[2]; } }; int main() { Solution m; m.allCellsDistOrder(1, 2, 0, 0); return 0; }