• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode812.最大三角形面积|LargestTriangleArea

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10562694.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

You have a list of points in the plane. Return the area of the largest triangle that can be formed by any 3 of the points.

Example:
Input: points = [[0,0],[0,1],[1,0],[0,2],[2,0]]
Output: 2
Explanation: 
The five points are show in the figure below. The red triangle is the largest.

Notes:

  • 3 <= points.length <= 50.
  • No points will be duplicated.
  •  -50 <= points[i][j] <= 50.
  • Answers within 10^-6 of the true value will be accepted as correct.

给定包含多个点的集合,从其中取三个点组成三角形,返回能组成的最大三角形的面积。

示例:
输入: points = [[0,0],[0,1],[1,0],[0,2],[2,0]]
输出: 2
解释: 
这五个点如下图所示。组成的橙色三角形是最大的,面积为2。

注意:

  • 3 <= points.length <= 50.
  • 不存在重复的点。
  •  -50 <= points[i][j] <= 50.
  • 结果误差值在 10^-6 以内都认为是正确答案。

24ms

 1 class Solution {
 2     func largestTriangleArea(_ points: [[Int]]) -> Double {
 3         var res:Double = 0
 4         for i in points
 5         {
 6             for j in points
 7             {
 8                 for k in points
 9                 {
10                     res = max(res, 0.5 * fabs(Double(i[0] * j[1] + j[0] * k[1] + k[0] * i[1] - j[0] * i[1] - k[0] * j[1] - i[0] * k[1])))
11                 }
12             }
13         }
14         return res
15     }
16 }

24ms

 1 class Solution {
 2     func largestTriangleArea(_ points: [[Int]]) -> Double {
 3         
 4         var result = 0.0
 5         for i in 0..<points.count {
 6             for j in i+1..<points.count {
 7                 for k in j+1..<points.count {
 8                     result = max(result, area(points[i], points[j], points[k]))
 9                 }
10             }
11         }
12         return result
13     }
14     
15     fileprivate func area(_ a:[Int], _ b: [Int], _ c:[Int]) -> Double {
16                 
17         
18             return 0.5 * abs(Double(a[0] * b[1] + b[0] * c[1] + c[0] * a[1]) -
19                              Double(a[1] * b[0] + b[1] * c[0] + c[1] * a[0]) )
20     }
21 }

36ms

 1 class Solution {
 2     func largestTriangleArea(_ points: [[Int]]) -> Double {
 3         var maxA = 0.0
 4         for i in 0..<points.count - 2 {
 5             for j in (i + 1)..<points.count - 1 {
 6                 for k in (j + 1)..<points.count {
 7                     maxA = max(maxA, area(points[i], points[j], points[k]))
 8                 }
 9             }
10         }
11         return maxA
12     }
13     
14     func area(_ point1: [Int], _ point2: [Int], _ point3: [Int]) -> Double {
15         let x1 = point1[0]
16         let y1 = point1[1]
17         let x2 = point2[0]
18         let y2 = point2[1]
19         let x3 = point3[0]
20         let y3 = point3[1]
21         return abs(0.5*Double(x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2)))
22     }
23 }

40ms

 1 class Solution {
 2     func largestTriangleArea(_ points: [[Int]]) -> Double {
 3         var maxArea = 0.0
 4         
 5         let cnt = points.count
 6         if cnt < 3 {
 7             return 0
 8         }
 9 
10         for i in 0..<cnt-2 {
11             for j in i+1..<cnt-1 {
12                 for k in j+1..<cnt {
13                     let x1 = points[i][0]
14                     let y1 = points[i][1]
15                     
16                     let x2 = points[j][0]
17                     let y2 = points[j][1]
18                     
19                     let x3 = points[k][0]
20                     let y3 = points[k][1]
21                     
22                     let temp = (x2-x1)*(y3-y1)-(x3-x1)*(y2-y1)
23                     let area = abs(0.5*Double(temp))
24 
25                     maxArea = max(maxArea, area)
26                 }
27             }
28         }
29 
30         return maxArea
31     }
32 }

88ms

 1 class Solution {
 2 
 3     let x = 0
 4     let y = 1
 5 
 6     func largestTriangleArea(_ points: [[Int]]) -> Double {
 7         var ans = 0.0
 8         for x in stride(from: 0, through: points.count - 3, by: 1) {
 9             for y in stride(from: x + 1, through: points.count - 2, by: 1) {
10                 for z in stride(from: y + 1, through: points.count - 1, by: 1) {
11                     let array = [points[x], points[y], points[z]]
12                     ans = max(ans, area(array))
13                 }
14             }
15         }
16         return ans
17     }
18 
19     private func area(_ points: [[Int]]) -> Double {
20         var ans = points[0][x] * points[1][y]
21         ans += points[1][x] * points[2][y]
22         ans += points[2][x] * points[0][y]
23 
24         ans -= points[0][y] * points[1][x]
25         ans -= points[1][y] * points[2][x]
26         ans -= points[2][y] * points[0][x]
27 
28         return abs(0.5 * Double(ans))
29     }
30 }

100ms

 1 class Solution {
 2     func area(_ x:[Int], _ y:[Int], _ z:[Int] ) -> Double{
 3         let a = Double(x[0]*y[1])
 4         let b = Double(y[0]*z[1])
 5         let c = Double(z[0]*x[1])
 6         let d = Double(x[0]*z[1])
 7         let e = Double(y[0]*x[1])
 8         let f = Double(z[0]*y[1])
 9         let g = a + b + c - d - e - f
10         return 0.5 * g
11     }
12     func largestTriangleArea(_ points: [[Int]]) -> Double {
13         var res:Double = 0
14         for i in 0..<points.count {
15             for j in 0..<points.count{
16                 if i == j {
17                     continue
18                 }
19                 for k in (j+1)..<points.count{
20                     if (k == i || k == j){
21                         continue
22                     }
23                     let temp:Double = area(points[i], points[j], points[k])
24                     if res < temp {
25                         res = temp
26                     }                    
27                 }
28             }
29         }
30         
31         return res
32     }
33 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap