Advertisement
Alexxik

Untitled

Sep 8th, 2023 (edited)
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.60 KB | None | 0 0
  1. // MARK: - 977. Squares of a Sorted Array
  2. // Указатели в начало и конец. Квадрат которого больше добавляем и сдвигаем
  3.  
  4. func sortedSquares(_ nums: [Int]) -> [Int] {
  5.     var result = [Int]()
  6.    
  7.     var l = 0
  8.     var r = nums.count - 1
  9.    
  10.     while l <= r {
  11.         if nums[r] * nums[r] > nums[l] * nums[l] {
  12.             result.append(nums[r] * nums[r])
  13.             r -= 1
  14.         } else {
  15.             result.append(nums[l] * nums[l])
  16.             l += 1
  17.         }
  18.     }
  19.     return result.reversed()
  20. }
  21.  
  22. sortedSquares([-7,-3,2,3,11])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement