Leecode:Squares of a Sorted Array(JS解答)
直接上代码:
function sortedSquares(nums: number[]): number[] {
var res:number[] = [];
var len:number = nums.length;
for(var i=0,j=len-1,temp=len-1;i <= j;){
if(nums[i] * nums[i] < nums[j] * nums[j]){
res[temp] = nums[j] * nums[j];
j--;
}else{
res[temp] = nums[i] * nums[i];
i++;
}
temp--;
}
return res
};