题目:
此题不用考虑太多,主要是考察几何知识,直接枚举即可。
三角形计算面积为:
假设这三个点,第一个点 i 在 points 数组左边,第二个点的数组位置位于两点之间,第三个点在右边
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| #include <cmath> #include <vector>
using std::vector;
class Solution { public: double largestTriangleArea(vector<vector<int>> &points) {
int n; int x1, x2, x3; int y1, y2, y3; double max_area = 0; double area;
n = points.size(); for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { for (int k = j + 1; k < n; k++) { x1 = points[i][0]; x2 = points[j][0]; x3 = points[k][0]; y1 = points[i][1]; y2 = points[j][1]; y3 = points[k][1]; area = 0.5 * std::fabs(x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)); if (area > max_area) { max_area = area; } } } } return max_area; } };
|