-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContents.swift
60 lines (42 loc) · 1.24 KB
/
Contents.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//: [上一道题](@previous)
/*:
# 两个数组的交集
- 题号:[349](https://leetcode-cn.com/problems/intersection-of-two-arrays/)
- 难度:简单
- 描述:
给定两个数组,编写一个函数来计算它们的交集。
*/
//: ## Code
import Foundation
func intersection(_ nums1: [Int], _ nums2: [Int]) -> [Int] {
return Array(Set(nums1).intersection(Set(nums2)))
}
func intersection2(_ nums1: [Int], _ nums2: [Int]) -> [Int] {
let _nums1 = nums1.sorted()
let _nums2 = nums2.sorted()
var left = 0
var righ = 0
var result: [Int] = []
while left < _nums1.count && righ < _nums2.count {
let _left = _nums1[left]
let _righ = _nums2[righ]
if _left == _righ {
left += 1
righ += 1
if result.firstIndex(of: _left) == nil {
result.append(_left)
}
continue
}
if _left < _righ {
left += 1
} else {
righ += 1
}
}
return result
}
//: ## Test
print(intersection2([1,2,2,1], [2,2])) // [2]
print(intersection2([4,9,5], [9,4,9,8,4])) // [9,4]
//: [下一道题](@next)