平面上有一系列点。返回由其中三个点可以形成的三角形最大面积。
样例:
输入: points = [[0,0],[0,1],[1,0],[0,2],[2,0]]
输出: 2
思路:已知三个点为(x1,y1),(x2,y2),(x3,y3)
面积为A= 1/2 * abs[ x1(y2-y3) + x2(y3-y1) + x3(y1-y2) ]
class Solution {
public:
/**
* @param points: List[List[int]]
* @return: return a double
*/
double largestTriangleArea(vector<vector<int>> &points) {
// write your code here
double sum=0;
double Max=0;
int len=points.size();
for (int i = 0; i < len; i++) {
for(int j=i+1;j<len;j++)
{
for(int k=j+1;k<len;k++)
{
sum=0.5*abs(points[i][0]*(points[j][1]-points[k][1])+points[j][0]*(points[k][1]-points[i][1])+points[k][0]*(points[i][1]-points[j][1]));
Max=max(sum,Max);
}
}
}
return Max;
}
};