在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ Given an array of integers Example 1: Input: [-4,-1,0,3,10]
Output: [0,1,9,16,100]
Example 2: Input: [-7,-3,2,3,11]
Output: [4,9,9,49,121]
Note:
给定一个按非递减顺序排序的整数数组 示例 1: 输入:[-4,-1,0,3,10] 输出:[0,1,9,16,100] 示例 2: 输入:[-7,-3,2,3,11] 输出:[4,9,9,49,121] 提示:
300ms 1 class Solution { 2 func sortedSquares(_ A: [Int]) -> [Int] { 3 if A.count == 1 { 4 return A.map{ 5 $0 * $0 6 } 7 } 8 9 var result: [Int] = [Int]() 10 var i: Int = 0 11 var j: Int = A.count - 1 12 13 while i != j { 14 if module(A[i]) >= module(A[j]) { 15 result.append(A[i] * A[i]) 16 i += 1 17 } else { 18 result.append(A[j] * A[j]) 19 j -= 1 20 } 21 } 22 result.append(A[i] * A[i]) 23 24 return result.reversed() 25 } 26 27 func module(_ number: Int) -> Int { 28 if number < 0 { 29 return -number 30 } else { 31 return number 32 } 33 } 34 } 308ms 1 class Solution { 2 func sortedSquares(_ A: [Int]) -> [Int] { 3 var ans = [Int]() 4 5 if let posI = A.firstIndex(where: { $0 >= 0 }) { 6 var left = posI - 1 7 var right = posI 8 9 while left >= 0 || right < A.count { 10 if left >= 0 && (right >= A.count || A[left] * A[left] < A[right] * A[right]) { 11 ans.append(A[left] * A[left]) 12 left -= 1 13 } else { 14 ans.append(A[right] * A[right]) 15 right += 1 16 } 17 } 18 } else { 19 // if no value is larger than 0 -> all of them are negative 20 for i in (0..<A.count).reversed() { 21 ans.append(A[i] * A[i]) 22 } 23 } 24 25 return ans 26 } 27 } 356ms 1 class Solution { 2 func sortedSquares(_ A: [Int]) -> [Int] { 3 var maxNumber = 0 4 var map:[Int:Int] = [:] 5 for i in 0 ..< A.count { 6 maxNumber = max(maxNumber, abs(A[i])) 7 let key = abs(A[i]) 8 if let v = map[key] { 9 map[abs(A[i])] = v + 1 10 }else { 11 map[abs(A[i])] = 1 12 } 13 } 14 var output:[Int] = [] 15 for i in 0 ... maxNumber { 16 if let count = map[i] { 17 let value = i*i 18 if count == 1 { 19 output.append(value) 20 } else { 21 output += Array(repeating: value, count: count) 22 } 23 } 24 } 25 return output 26 } 27 } 376ms 1 class Solution { 2 func sortedSquares(_ A: [Int]) -> [Int] { 3 return A.map({$0 * $0}).sorted() 4 } 5 } 380ms 1 class Solution { 2 func sortedSquares(_ A: [Int]) -> [Int] { 3 var nonSortedArray = [Int]() 4 for value in A { 5 nonSortedArray.append(value * value) 6 } 7 8 return nonSortedArray.sorted() 9 } 10 } 400ms 1 class Solution { 2 func sortedSquares(_ A: [Int]) -> [Int] { 3 return A.reduce(into: [], { $0.append($1 * $1) }).sorted(by: <) 4 } 5 } 404ms 1 class Solution { 2 func sortedSquares(_ A: [Int]) -> [Int] { 3 return A.lazy.map{$0 * $0}.sorted(by: {$0 < $1}) 4 } 5 } 416ms 1 class Solution { 2 func sortedSquares(_ A: [Int]) -> [Int] { 3 return A.map { Int(pow(Double($0), 2)) }.sorted() 4 } 5 } 6284ms 1 class Solution { 2 func sortedSquares(_ A: [Int]) -> [Int] { 3 var A = A.map{ abs($0) } 4 func fastSort(_ B: [Int]) -> [Int] { 5 guard !B.isEmpty else { return [] } 6 let midIndex = B.count/2 7 return fastSort(Array(B.filter{ $0 < B[midIndex] })) + Array(B.filter{ $0 == B[midIndex] }) + fastSort(Array(B.filter{ $0 > B[midIndex] })) 8 } 9 return fastSort(A).map{ $0*$0 } 10 } 11 }
|
请发表评论