在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ Given two arrays Sort the elements of 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 = [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] 提示:
8ms
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 Memory Usage: 21.2 MB
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 }
|
请发表评论