My Swift solution O(nlogn) 32ms


  • 0
    2
        func intersect(nums1: [Int], _ nums2: [Int]) -> [Int] {
           var result = [Int]()
           let sortedNums1 = nums1.sort()
           let sortedNums2 = nums2.sort()
           var i = 0
           var j = 0
           while i != sortedNums1.count && j != sortedNums2.count
           {
               let a = sortedNums1[i]
               let b = sortedNums2[j]
               if a == b {
                   result.append(a)
                   i += 1
                   j += 1
               }
               else if a > b {
                   j += 1
               }
               else {
                   i += 1
               }
           }
           return result
        }
    }```

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.