llllmz

导航

66. 加一c

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
void reverse(int* s,int n){
    int head=0,tail=n-1;
    while(head<=tail){
        int c=s[head];
        s[head]=s[tail];
        s[tail]=c;
        head++;
        tail--;
    }
}

int* plusOne(int* digits, int digitsSize, int* returnSize) {
    int n=digitsSize;
    reverse(digits,n);
    int* array=(int*)malloc(sizeof(int)*(n+2));
    int index=0,pre=1;
    while(index<n){
        int temp=(digits[index]+pre)%10;
        pre=(digits[index]+pre)/10;
        array[index++]=temp;
    }
    if(pre==1) array[index++]=1;
    reverse(array,index);
    (*returnSize)=index;
    return array;
}

 

posted on 2024-03-21 20:24  神奇的萝卜丝  阅读(2)  评论(0编辑  收藏  举报