Title: Square ordered arrays


Given an array of integers A sorted in non-descending order, return A new array of the squares of each number, also sorted in non-descending order.Copy the code

Example:


Input: [4, 1,0,3,10] output:,1,9,16,100 [0] input: [7, 3,2,3,11] output:,9,9,49,121 [4]Copy the code

Think about:


Create a new array, place the square of the elements in the array, and sort it.Copy the code

Implementation:


class Solution {
public int[] sortedSquares(int[] A) {
    int N = A.length;
    int[] ans = new int[N];
    for (int i = 0; i < N; ++i)
        ans[i] = A[i] * A[i];

    Arrays.sort(ans);
    return ans;
}
Copy the code

}