[Swift]LeetCode532. 数组中的K-diff数对 | K-diff Pairs in an Array

[Swift]LeetCode532. 数组中的K-diff数对 | K-diff Pairs in an Array,第1张

概述Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an integer pair (i, j), where i and j are both numbers in

Given an array of integers and an integer k,you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an integer pair (i,j),where i and j are both numbers in the array and their absolute difference is k.

Example 1:

input: [3,1,4,5],k = 2Output: 2Explanation: There are two 2-diff pairs in the array,(1,3) and (3,5).
Although we have two 1s in the input,we should only return the number of unique pairs.

 Example 2:

input:[1,2,3,k = 1Output: 4Explanation: There are four 1-diff pairs in the array,2),(2,3),(3,4) and (4,5).

 Example 3:

input: [1,5,4],k = 0Output: 1Explanation: There is one 0-diff pair in the array,1).

 Note:

The pairs (i,j) and (j,i) count as the same pair. The length of the array won‘t exceed 10,000. All the integers in the given input belong to the range: [-1e7,1e7].

1
class Solution { 2 func findPairs(_ nums: [Int],_ k: Int) -> Int { 3 var count:Int = 0 4 if k < 0 {return count} 5 var hm:[Int:Int] = [Int:Int]() 6 let len:Int = nums.count 7 for i in 0..<len 8 { 9 hm[nums[i]] = i10 }11 for i in 0..<len12 {13 //如果字典包含所请求键的值,则下标返回包含该键的现有值的可选值。否则,下标返回nil:14 if let keys = hm[nums[i] + k] 15 {16 if keys != i17 {18 count += 119 //使用下标语法通过nil为该键指定值来从字典中删除键值对20 //使用该removeValue(forKey:)方法从字典中删除键值对。hm.removeValue(forKey:key)21 //此方法删除键值对(如果存在)并返回已删除的值,nil如果不存在值则返回22 hm[nums[i] + k] = nil 23 }24 }25 }26 return count27 }28 }

44ms

 1 class Solution { 2     func findPairs(_ nums: [Int],_ k: Int) -> Int { 3         guard k >= 0 else { 4             return 0 5         } 6          7         var dict = [Int: Int]() 8         for num in nums { 9             //dict[num] = dict[num] == nil ? 0 : dict[num]! + 110             dict[num,default: 0] += 111         }12         13         var res = 014         for (num,count) in dict {15             if k == 0 {16                 if count > 1 {17                     res += 118                 } 19             } else {20                 if dict[num - k] != nil {21                     res += 122                 }23             }24             25         }26         return res27     }28 }

48ma

 1 class Solution { 2     func findPairs(_ nums: [Int],_ k: Int) -> Int { 3         guard nums.count > 1 else { return 0 } 4         guard k >= 0 else { return 0 } 5  6         var res = 0 7         var dict = [Int: Int]() 8         for n in nums { 9             if let val = dict[n] {10                 dict[n] = val + 111             } else {12                 dict[n] = 113             }14         }15         16         if k == 0 {17             return dict.reduce(0) { (r,arg1) -> Int in18                 if arg1.value > 1 {  return r + 1}19                 return r20             }21         } else {22             for key in dict.keys {23                 if dict.keys.contains(key + k) {24                     res = res + 125                 }26             }27             return res28         }29     }30 }

52ms

 1 class Solution { 2     func findPairs(_ nums: [Int],_ k: Int) -> Int { 3         guard k >= 0 else { 4             return 0 5         } 6          7         var dict = [Int: Int]() 8         for num in nums { 9             dict[num,default: 0] += 110         }11         12         var res = 013         for (num,count) in dict {14             if k == 0 {15                 if count > 1 {16                     res += 117                 } 18             } else {19                 if dict[num - k] != nil {20                     res += 121                 }22             }23             24         }25         return res26     }27 }

56ms

 1 class Solution { 2     func findPairs(_ nums: [Int],_ k: Int) -> Int { 3         var dict = [Int: Bool]() 4          5         if k < 0 { 6             return 0 7         } 8          9         var diff = k < 0 ? -1*k : k10         var count = 011         12         13         14         for num in nums {15             if !dict.keys.contains(num) {16                 dict[num] = false17                 if diff == 0 { continue }18             }19             20             if dict.keys.contains(num-diff) {21                 if dict[num-diff] == false {22                     dict[num-diff] = true23                     count = count + 124                 }25                 26             } 27             if dict.keys.contains(num+diff) {28                 if dict[num] == false {29                     dict[num] = true30                     count = count + 131                 }32             }33         }34         35         return count36     }37 }

68ms

 1 class Solution { 2     func findPairs(_ nums: [Int],_ k: Int) -> Int { 3          4         guard k >= 0 else { return 0 } 5          6         var dict = nums.reduce(into: [Int: Int]()) { dict,num in 7             dict[num,default:0] += 1 8         } 9         10         guard k > 0 else {11             return dict.reduce(into: 0) { count,numAndCount in12                 if numAndCount.1 > 1 {13                     count += 114                 }15             }16         }17         18         var totalCount = 019         for (num,count) in dict {20             if dict[num+k,default:0] > 0 {21                 totalCount += 122             }23             if dict[num-k,default: 0] > 0 {24                 totalCount += 125             }26             dict[num] = 027         }28         29         return totalCount30     }31 }

76ms

 1 class Solution { 2     func findPairs(_ nums: [Int],default:0] += 1 8         } 9         10         if k == 0 {11             return dict.values.reduce(into: 0) { total,appearances in12                 if appearances > 1 {13                     total += 114                 }15             }16         }17         18         return dict.keys.reduce(into: 0) { total,num in19             if dict[num+k,default:0] > 0 {20                 total += 121             }22             if dict[num-k,default: 0] > 0 {23                 total += 124             }25             dict[num] = 026         }27         28     }29 }

96ms

 1 class Solution { 2     func findPairs(_ nums: [Int],_ k: Int) -> Int { 3         guard nums.count > 1 else { return 0} 4         let sortednums = nums.sorted() 5         var behind = 0 6         var forward = 1 7         var counter = 0 8  9         while forward < nums.count,behind < nums.count {10             if behind >= forward {11                 forward = behind + 112                 continue13             }14             15             let diff = abs(sortednums[forward] - sortednums[behind])16             17             if diff < k {18                 repeat { forward += 1} while forward < nums.count && sortednums[forward] == sortednums[forward-1]19                 continue20             }21 22             if diff == k {23                 counter += 124                 repeat { forward += 1} while forward < nums.count && sortednums[forward] == sortednums[forward-1]25                 repeat { behind += 1} while behind < nums.count && sortednums[behind] == sortednums[behind-1]26                 continue27             }28 29             if diff > k {30                 repeat { behind += 1} while behind < nums.count && sortednums[behind] == sortednums[behind-1]31                 continue32             }33         }34         return counter35     }36 }

116ms

 1 class Solution { 2     func findPairs(_ nums: [Int],_ k: Int) -> Int { 3         let nums =  nums.sorted() 4         var i = 0 5         var j = 1 6         var res = 0 7         while  i < nums.count && j < nums.count  { 8             if nums[j] - nums[i] == k { 9                 res += 110                 i += 111                 j += 112                 while j < nums.count && nums[j] == nums[j-1]  {13                     j += 114                 }15             }else if nums[j] - nums[i] < k {16                 j += 117             }else  {18                 i += 119                 j = i < j ? j : i+120             }21         }22         return res23     }24 }

132ms

 1 class Solution { 2     func findPairs(_ nums: [Int],_ k: Int) -> Int { 3         guard nums.count > 1 else { 4             return 0 5         } 6         var resultSet: [[Int]] = [] 7         var sortednums = nums 8         sortednums.sort() 9         var firstPointer = 010         var secondPointer = 111         while secondPointer < sortednums.count {12             // print("comparing : \(sortednums[firstPointer]) and \(sortednums[secondPointer])")13             if abs(sortednums[firstPointer] - sortednums[secondPointer]) == k {14                 var valIDList: [Int] = [sortednums[firstPointer],sortednums[secondPointer]]15                 // if !resultSet.contains(where: {16 == valIDList}) {                    resultSet.append(valIDList) 17//                  }181                 firstPointer += 19else             } if  k { (sortednums[secondPointer] - sortednums[firstPointer]) <201                 secondPointer += 21else             }  {221                 firstPointer += 23            } 24if              secondPointer { firstPointer ==251                 secondPointer += 26            } 27        } 28"         print(This is the result set : \(resultSet)")29var          [] removeIndex:[Int] =30if         1 resultSet.count >  {31for             in index 1 resultSet.count {..<32//                  print("index : \(index)")33if                 1 resultSet[index] == resultSet[index-] {34                    removeIndex.append(index) 35                } 36            } 37        } 38while         0 removeIndex.count !=  {39//              print("removing \(removeIndex.last) as \(removeIndex)")40)             resultSet.remove(at: removeIndex.last!41 removeIndex.popLast()             let _ =42        } 43return          resultSet.count44    } 45 }
总结

以上是内存溢出为你收集整理的[Swift]LeetCode532. 数组中的K-diff数对 | K-diff Pairs in an Array全部内容,希望文章能够帮你解决[Swift]LeetCode532. 数组中的K-diff数对 | K-diff Pairs in an Array所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1023449.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存