leetcode【 977 square of ordered tree】

Insert picture description here
Double pointer is over!

int* sortedSquares(int* A, int ASize, int* returnSize) {
    
    
    int* ans = malloc(sizeof(int) * ASize);
    *returnSize = ASize;
    for (int i = 0, j = ASize - 1, pos = ASize - 1; i <= j;) 
    {
    
    
        if (A[i] * A[i] > A[j] * A[j]) 
        {
    
    
            ans[pos] = A[i] * A[i];
            ++i;
        } 
        else 
        {
    
    
            ans[pos] = A[j] * A[j];
            --j;
        }
        --pos;
    }
    return ans;
}

Guess you like

Origin blog.csdn.net/qq_45657288/article/details/109126408