leetcode-剑指66-OK

// language c
// 剑指66
// https://leetcode-cn.com/problems/gou-jian-cheng-ji-shu-zu-lcof/

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* constructArr(int* a, int aSize, int* returnSize){
    returnSize[0] = aSize;
    if(aSize==0)
        return a;
    if(aSize==1){
        a[0] = 1;
        return a;
    }
    int *ans = (int *)malloc(sizeof(int) * aSize);
    ans[aSize-1] = a[aSize-1];
    for(int i=1;i<aSize; i++){
        ans[aSize-i-1] = ans[aSize-i]*a[aSize-i-1];
    }
    for(int i=1;i<aSize; i++){
        a[i] *= a[i-1];
    }
    int start= ans[1];
    int end =  a[aSize-2];
    for(int i=1; i<aSize-1;i++)
        ans[i] = a[i-1] *ans[i+1];
    ans[0] = start;
    ans[aSize-1] = end;
    return ans;
}
posted @ 2021-01-28 05:42  RougeBW  阅读(40)  评论(0编辑  收藏  举报