Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // MARK: - 977. Squares of a Sorted Array
- // Указатели в начало и конец. Квадрат которого больше добавляем и сдвигаем
- func sortedSquares(_ nums: [Int]) -> [Int] {
- var result = [Int]()
- var l = 0
- var r = nums.count - 1
- while l <= r {
- if nums[r] * nums[r] > nums[l] * nums[l] {
- result.append(nums[r] * nums[r])
- r -= 1
- } else {
- result.append(nums[l] * nums[l])
- l += 1
- }
- }
- return result.reversed()
- }
- sortedSquares([-7,-3,2,3,11])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement