-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeet350.swift
More file actions
41 lines (34 loc) · 874 Bytes
/
Leet350.swift
File metadata and controls
41 lines (34 loc) · 874 Bytes
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
//
// Leet350.swift
// Algorithm
//
// Created by dabeen on 2022/05/18.
//
import Foundation
class Solution {
func intersec_1(_ nums1: [Int], _ nums2: [Int]) -> [Int] {
var answer: [Int] = []
var nums2 = nums2
for n in nums1 {
if nums2.isEmpty { break }
if let index = nums2.firstIndex(of: n) {
answer.append(nums2.remove(at: index))
}
}
return answer
}
func intersect_2(_ nums1: [Int], _ nums2: [Int]) -> [Int] {
var answer: [Int] = []
var numDic: [Int:Int] = [:]
for n in nums1 {
numDic[n,default: 0] += 1
}
for n in nums2 {
if let count = numDic[n] {
answer.append(n)
numDic[n] = count == 1 ? nil : count-1
}
}
return answer
}
}