[Swift]LeetCode1122. 数组的相对排序 | Relative Sort Array
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/11179476.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
Given two arrays arr1
and arr2
, the elements of arr2
are distinct, and all elements in arr2
are also in arr1
.
Sort the elements of arr1
such that the relative ordering of items in arr1
are the same as in arr2
. Elements that don't appear in arr2
should be placed at the end of arr1
in ascending order.
Example 1:
Input: arr1 = [2,3,1,3,2,4,6,7,9,2,19], arr2 = [2,1,4,3,9,6] Output: [2,2,2,1,4,3,3,9,6,7,19]
Constraints:
arr1.length, arr2.length <= 1000
0 <= arr1[i], arr2[i] <= 1000
- Each
arr2[i]
is distinct. - Each
arr2[i]
is inarr1
.
给你两个数组,arr1
和 arr2
,
arr2
中的元素各不相同arr2
中的每个元素都出现在arr1
中
对 arr1
中的元素进行排序,使 arr1
中项的相对顺序和 arr2
中的相对顺序相同。未在 arr2
中出现过的元素需要按照升序放在 arr1
的末尾。
示例:
输入:arr1 = [2,3,1,3,2,4,6,7,9,2,19], arr2 = [2,1,4,3,9,6] 输出:[2,2,2,1,4,3,3,9,6,7,19]
提示:
arr1.length, arr2.length <= 1000
0 <= arr1[i], arr2[i] <= 1000
arr2
中的元素arr2[i]
各不相同arr2
中的每个元素arr2[i]
都出现在arr1
中
1 class Solution { 2 func relativeSortArray(_ arr1: [Int], _ arr2: [Int]) -> [Int] { 3 var dict = [Int: Int]() 4 var result = [Int]() 5 for a in arr1 { 6 dict[a] = (dict[a] ?? 0)+1 7 } 8 9 for a in arr2 { 10 if let val = dict[a] { 11 for i in 0..<val { 12 result.append(a) 13 } 14 dict.removeValue(forKey: a) 15 } 16 } 17 var simpleArray = [Int]() 18 if !dict.isEmpty { 19 for (key,val) in dict { 20 for i in 0..<val { 21 simpleArray.append(key) 22 } 23 dict.removeValue(forKey: key) 24 } 25 } 26 result.append(contentsOf: simpleArray.sorted()) 27 return result 28 } 29 }
12ms
1 class Solution { 2 func relativeSortArray(_ arr1: [Int], _ arr2: [Int]) -> [Int] { 3 var dict = Dictionary<Int,Int>() 4 5 for value in arr1 { 6 if dict[value] == nil { 7 dict[value] = 1 8 }else { 9 dict[value] = dict[value]! + 1 10 } 11 } 12 13 14 var outputArray: Array<Int> = Array<Int>() 15 16 for value in arr2 { 17 18 if let count = dict[value] { 19 for _ in 1...count{ 20 outputArray.append(value) 21 } 22 } 23 } 24 25 let set2 = Set(arr2) 26 27 var appendArray = Array<Int>() 28 29 for value in arr1 { 30 if !set2.contains(value) { 31 appendArray.append(value) 32 } 33 } 34 appendArray.sort() 35 outputArray.append(contentsOf: appendArray) 36 return outputArray 37 } 38 }
16ms
1 class Solution { 2 func relativeSortArray(_ arr1: [Int], _ arr2: [Int]) -> [Int] { 3 let (numToCount, numsNotInArr2) = getNumToCount(arr1, arr2) 4 var result = [Int]() 5 for num in arr2 { 6 if let count = numToCount[num] { 7 for _ in 0..<count { 8 result.append(num) 9 } 10 } 11 } 12 result.append(contentsOf: numsNotInArr2) 13 return result 14 } 15 16 private func getNumToCount(_ arr1: [Int], _ arr2: [Int]) -> ([Int:Int], [Int]) { 17 let arr2Set = Set(arr2) 18 var numToCount = [Int:Int]() 19 var numsNotInArr2 = [Int]() 20 for num in arr1 { 21 if !arr2Set.contains(num) { 22 numsNotInArr2.append(num) 23 } else { 24 numToCount.updateValue((numToCount[num] ?? 0) + 1, forKey: num) 25 } 26 } 27 numsNotInArr2 = numsNotInArr2.sorted() 28 return (numToCount, numsNotInArr2) 29 } 30 }
Runtime: 20 ms
1 class Solution { 2 func relativeSortArray(_ arr1: [Int], _ arr2: [Int]) -> [Int] { 3 var arr1 = arr1.sorted(by:<) 4 var n:Int = arr1.count 5 var used:[Bool] = [Bool](repeating:false,count:n) 6 var result:[Int] = [Int]() 7 for x in arr2 8 { 9 for i in 0..<n 10 { 11 if arr1[i] == x && !used[i] 12 { 13 result.append(x) 14 used[i] = true 15 } 16 } 17 } 18 for i in 0..<n 19 { 20 if !used[i] 21 { 22 result.append(arr1[i]) 23 } 24 } 25 return result 26 } 27 }
20ms
1 class Solution { 2 func relativeSortArray(_ arr1: [Int], _ arr2: [Int]) -> [Int] { 3 4 var orderMap = [Int: Int]() 5 6 for (offset, element) in arr2.enumerated() { 7 orderMap[element] = offset 8 } 9 10 let result = arr1.sorted { left, right in 11 switch (orderMap[left], orderMap[right]) { 12 case (let leftOrder?, let rightOrder?): 13 return leftOrder < rightOrder 14 case (.none, .some): 15 return false 16 case (.some, .none): 17 return true 18 default: 19 return left < right 20 } 21 } 22 return result 23 } 24 }
24ms
1 class Solution { 2 func relativeSortArray(_ arr1: [Int], _ arr2: [Int]) -> [Int] { 3 var counter = [Int : Int]() 4 for num in arr1 { 5 counter[num, default: 0] += 1 6 } 7 8 var ans = [Int]() 9 for num in arr2 { 10 ans += Array(repeating: num, count: counter[num]!) 11 counter[num] = nil 12 } 13 14 let nums = counter.keys.sorted() 15 for num in nums { 16 ans += Array(repeating: num, count: counter[num]!) 17 counter[num] = nil 18 } 19 20 return ans 21 } 22 }
28ms
1 class Solution { 2 func relativeSortArray(_ arr1: [Int], _ arr2: [Int]) -> [Int] { 3 var dict = [Int: Int]() 4 for i in 0..<arr2.count { 5 dict[arr2[i]] = i 6 } 7 8 return arr1.sorted(by: { (e1, e2) in 9 10 guard let idx1 = dict[e1] else { 11 if dict[e2] == nil { return e1 < e2 } 12 return false 13 } 14 guard let idx2 = dict[e2] else { 15 return true 16 } 17 return idx1 < idx2 18 }) 19 20 } 21 }
52ms
1 class Solution { 2 func relativeSortArray(_ arr1: [Int], _ arr2: [Int]) -> [Int] { 3 var result = [Int]() 4 var haystack = arr1 5 for needle in arr2 { 6 var index = 0 7 for element in haystack { 8 guard needle == element else { index += 1; continue } 9 haystack.remove(at: index) 10 result.append(element) 11 } 12 } 13 result.append(contentsOf: haystack.sorted()) 14 return result 15 } 16 }