Created
April 17, 2022 08:22
-
-
Save MohammedALREAI/00f696845230274b8d18613068faee11 to your computer and use it in GitHub Desktop.
leetcode 977. Squares of a Sorted Array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sortedSquares(nums: number[]): number[] { | |
let result = Array(nums.length).fill(0) | |
let p1 = 0 | |
let p2 = nums.length - 1 | |
let position = p2 | |
while (p1 <= p2) { | |
let smaller=nums[p1] | |
let laege=nums[p2] | |
if( Math.abs(smaller)>Math.abs(laege)){ | |
result[position] = smaller*smaller | |
position--; | |
p1++ | |
} else { | |
result[position] = laege*laege | |
position-- | |
p2-- | |
} | |
} | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment