★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-ujlozzip-kw.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Given two arrays arr1
and arr2
, the elements of arr2
are distinct, and all elements in arr2
are also in arr1
.git
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. github
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
arr2[i]
is distinct.arr2[i]
is in arr1
.給你兩個數組,arr1
和 arr2
,app
arr2
中的元素各不相同arr2
中的每一個元素都出如今 arr1
中對 arr1
中的元素進行排序,使 arr1
中項的相對順序和 arr2
中的相對順序相同。未在 arr2
中出現過的元素須要按照升序放在 arr1
的末尾。 spa
示例:code
輸入: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]
提示:htm
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 }
12msblog
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 }
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 }