You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order.
Here is O(n) solution because the intuitive version is trivial
/** * @param {number[]} nums * @return {number[]} */varsortedSquares=function(nums){//1. We declare a result array firstletres=[];//2. Put two pointers at the front and endletl=0;letr=nums.length-1;//3. Declare a special pointer 'p' for the result array only, we should make p points to the last of the array instead of 1st index, because absolute value of a negative number may become very large letp=r;while(l<=r){if(nums[l]**2>nums[r]**2){res[p]=nums[l]**2;l++;p--;}else{res[p]=nums[r]**2;r--;p--;}}returnres;};
The text was updated successfully, but these errors were encountered:
Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order.
Here is O(n) solution because the intuitive version is trivial
The text was updated successfully, but these errors were encountered: