zoukankan      html  css  js  c++  java
  • [Swift]LeetCode812. 最大三角形面积 | Largest Triangle Area

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

    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 }
  • 相关阅读:
    解决帝国CMS搜索页面模板不支持灵动标签和万能标签的方法
    Visual Studio Code 相关
    随机的背景图案
    将视频做为网页背景 超炫!
    gunicorn部署Flask服务
    查看mysql数据库及表编码格式
    查看修改mysql编码方式
    【ssm整合打印sql语句】
    【mybatis在控制台打印sql语句配置】
    【mybatis 的foreach的用法】
  • 原文地址:https://www.cnblogs.com/strengthen/p/10562694.html
Copyright © 2011-2022 走看看