此题也是 对二分法的考察。通过首尾两个指针来寻找当前最大值。
javascript
/**
* @param {number[]} nums
* @return {number[]}
*/
var sortedSquares = function(nums) {
let start = 0;
let end = nums.length - 1;
const results = new Array(nums.length)
let newIndex = results.length - 1
while(start <= end) {
const startValue = Math.abs(nums[start])
const endValue = Math.abs(nums[end])
if (startValue > endValue) {
results[newIndex] = startValue * startValue
start++;
newIndex--;
} else {
results[newIndex] = endValue * endValue
end--;
newIndex--;
}
}
return results
};