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

[Swift]LeetCode593.有效的正方形|ValidSquare

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

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

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

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

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

Given the coordinates of four points in 2D space, return whether the four points could construct a square.

The coordinate (x,y) of a point is represented by an integer array with two integers.

Example:

Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1]
Output: True 

Note:

  1. All the input integers are in the range [-10000, 10000].
  2. A valid square has four equal sides with positive length and four equal angles (90-degree angles).
  3. Input points have no order.

给定二维空间中四点的坐标,返回四点是否可以构造一个正方形。

一个点的坐标(x,y)由一个有两个整数的整数数组表示。

示例:

输入: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1]
输出: True 

注意:

  1. 所有输入整数都在 [-10000,10000] 范围内。
  2. 一个有效的正方形有四个等长的正长和四个等角(90度角)。
  3. 输入点没有顺序。

Runtime: 8 ms
Memory Usage: 19.2 MB
 1 class Solution {
 2     func validSquare(_ p1: [Int], _ p2: [Int], _ p3: [Int], _ p4: [Int]) -> Bool {
 3         var s:Set<Int> = [d(p1, p2), d(p1, p3), d(p1, p4), d(p2, p3), d(p2, p4), d(p3, p4)]
 4         return !s.contains(0) && s.count == 2
 5     }
 6     
 7     func d(_ p1:[Int],_ p2:[Int]) -> Int
 8     {
 9          return (p1[0] - p2[0]) * (p1[0] - p2[0]) + (p1[1] - p2[1]) * (p1[1] - p2[1])        
10     }
11 }

12ms

 1 class Solution {
 2     func validSquare(_ p1: [Int], _ p2: [Int], _ p3: [Int], _ p4: [Int]) -> Bool {
 3         var dict = [Int: Int]()
 4         let distances = [dist(p1, p2), dist(p1, p3), dist(p1, p4), dist(p2, p3), dist(p2, p4), dist(p3, p4)]
 5         var maxDist = -1
 6         for dist in distances {
 7             maxDist = max(maxDist, dist)
 8             dict[dist] = dict[dist, default: 0] + 1
 9         }
10         if (dict[maxDist] == 2 && dict.count == 2) {
11             return true
12         }
13         return false
14     }
15     
16     func dist(_ p1: [Int], _ p2: [Int]) -> Int {
17         return (p1[0]-p2[0]) * (p1[0]-p2[0]) + (p1[1]-p2[1]) * (p1[1]-p2[1])
18     }
19 }

16ms

 1 import Foundation
 2 
 3 class Solution {
 4     func validSquare(_ p1: [Int], _ p2: [Int], _ p3: [Int], _ p4: [Int]) -> Bool {       
 5         let points : [[Int]] = [p1,p2,p3,p4]
 6         var totalsum = 0
 7         var sums : [Double] = []
 8         
 9         if Set(points).count != points.count {
10             return false
11         }
12         
13         func get_distance(_ d1:[Int],_ d2: [Int]) -> Double {
14             let dist = sqrt(pow((Double(d1[0])-Double(d2[0])),2)+pow((Double(d1[1])-Double(d2[1])),2))
15             return dist
16         }
17         
18         for i in Range(0...points.count - 1) {
19             if i == points.count - 1 {
20                 break
21             }
22             for j in Range(i+1...points.count - 1) {
23                 let d = get_distance(points[i],points[j])
24                 sums.append(d)
25             }
26         }
27         if Set(sums).count == 2 {
28             return true
29         } else {
30             return false
31         }
32     }
33 }

20ms

 1 class Solution {
 2     func validSquare(_ p1: [Int], _ p2: [Int], _ p3: [Int], _ p4: [Int]) -> Bool {
 3         
 4         var distanceArray: [Int] = []
 5         
 6         distanceArray.append(distanceBetween(p1: p1, p2: p2))
 7         distanceArray.append(distanceBetween(p1: p1, p2: p3))
 8         distanceArray.append(distanceBetween(p1: p1, p2: p4))
 9         distanceArray.append(distanceBetween(p1: p2, p2: p3))
10         distanceArray.append(distanceBetween(p1: p2, p2: p4))
11         distanceArray.append(distanceBetween(p1: p3, p2: p4))
12         
13         distanceArray.sort()
14         
15         if distanceArray[0] > 0
16             , distanceArray[0] == distanceArray[1]
17             , distanceArray[1] == distanceArray[2]
18             , distanceArray[2] == distanceArray[3]
19             , distanceArray[4] == distanceArray[5] {
20             return true
21         } else {
22             return false
23         }
24     }
25     
26     private func distanceBetween(p1: [Int], p2: [Int]) -> Int {
27         let deltaX = p2.first! - p1.first!
28         let deltaY = p2.last! - p1.last!
29         return deltaX * deltaX + deltaY * deltaY
30     }
31 }

 


鲜花

握手

雷人

路过

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

请发表评论

全部评论

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

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

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

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

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