zoukankan      html  css  js  c++  java
  • [Swift]LeetCode349. 两个数组的交集 | Intersection of Two Arrays

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

    Given two arrays, write a function to compute their intersection.

    Example 1:

    Input: nums1 = [1,2,2,1], nums2 = [2,2]
    Output: [2]
    

    Example 2:

    Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
    Output: [9,4]

    Note:

    • Each element in the result must be unique.
    • The result can be in any order.

    给定两个数组,编写一个函数来计算它们的交集。

    示例 1:

    输入: nums1 = [1,2,2,1], nums2 = [2,2]
    输出: [2]
    

    示例 2:

    输入: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
    输出: [9,4]

    说明:

    • 输出结果中的每个元素一定是唯一的。
    • 我们可以不考虑输出结果的顺序。

    12ms

     1 class Solution {
     2     func intersection(_ nums1: [Int], _ nums2: [Int]) -> [Int] {
     3         var pool: [Int: Int] = [:]
     4         for num in nums1 {
     5             pool[num] = 1
     6         }
     7         
     8         var result: [Int] = []
     9         for num in nums2 {
    10             if pool[num] != nil {
    11                 pool[num] = nil
    12                 result.append(num)
    13             }
    14         }
    15         
    16         return result
    17     }
    18 }

    16ms

     1 class Solution {
     2     func intersection(_ nums1: [Int], _ nums2: [Int]) -> [Int] {
     3     if nums1.count == 0 || nums2.count == 0 {
     4         return [];
     5     }
     6 
     7     var intersectionDic:[Int:Bool] = Dictionary();
     8 
     9     for number in nums1 {
    10         intersectionDic[number] = true
    11     }
    12 
    13     var intersectionArray:[Int] = Array();
    14     for  number in nums2  {
    15         let hasIntersection = intersectionDic[number] ?? false
    16         guard hasIntersection else {continue}
    17 
    18         intersectionArray.append(number)
    19         intersectionDic[number] = false
    20     }
    21 
    22     return intersectionArray;
    23     }
    24 }
     1 class Solution {
     2     func intersection(_ nums1: [Int], _ nums2: [Int]) -> [Int] {
     3         //利用Set的基本集合操作
     4         var set1:Set<Int> = Set(nums1)
     5         var set2:Set<Int> = Set(nums2)
     6         var res:Set<Int> = set1.intersection(set2)
     7         //Set转Array
     8         return Array(res)
     9     }
    10 }
    1 class Solution {
    2     func intersection(_ nums1: [Int], _ nums2: [Int]) -> [Int] {        
    3         let intersect = Set(nums1).intersection(Set(nums2))
    4         return Array(intersect)
    5     }
    6 }

    20ms

     1 class Solution {
     2     func intersection(_ nums1: [Int], _ nums2: [Int]) -> [Int] {
     3         var sortedNums1 = nums1.sorted()
     4         var sortedNums2 = nums2.sorted()
     5         var ret: Set<Int> = []
     6         var i = 0
     7         var j = 0
     8         
     9         while i < sortedNums1.count && j < sortedNums2.count {
    10             if sortedNums1[i] > sortedNums2[j] {
    11                 j+=1
    12             } else if sortedNums1[i] < sortedNums2[j] {
    13                 i+=1
    14             } else {
    15                 ret.insert(sortedNums1[i])
    16                 i+=1
    17                 j+=1
    18             }
    19         }
    20         
    21         return Array(ret)
    22     }
    23 }

    24ms

     1 class Solution {
     2     func intersection(_ nums1: [Int], _ nums2: [Int]) -> [Int] {
     3         let s = Set<Int>.init(nums1)
     4         var result = Set<Int>()
     5         for value in nums2 {
     6             if(s.contains(value) && !result.contains(value)){
     7                 result.insert(value)
     8             }
     9         }
    10         return Array<Int>.init(result)
    11     }
    12 }
  • 相关阅读:
    转:选择好友的下拉控件(类型开心网的)
    转:Silverlight样式写法
    转:构建无坚不摧的网站环境—NLB+Cluster(一)
    SQL2005 Collate问题
    转:写HTML邮件的建议及规范
    转:extjs里的fieldset不居中的解决办法(记录)
    转:大规模网站架构技术原理透析
    转:关于大型asp.net应用系统的架构架构的选择
    转:构建无坚不摧的网站环境——NLB+Cluster(二)
    ASP.NET MVC 2.0在WinXP IIS6下的部署
  • 原文地址:https://www.cnblogs.com/strengthen/p/9768962.html
Copyright © 2011-2022 走看看