新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

LeetCode066PlusOneC语言

Given a non-negative number represented as an array of digits, plus one to the number.
The digits are stored such that the most significant digit is at the head of the list.

题意:一个非负的整数用数组的形式保存着。其中高位在a[0]。然后对这个数做加1操作,返回这个数组。

网站建设公司,为您提供网站建设,网站制作,网页设计及定制网站建设服务,专注于企业网站制作,高端网页制作,对湿喷机等多个行业拥有丰富的网站建设经验的网站建设公司。专业网站设计,网站优化推广哪家好,专业营销推广优化,H5建站,响应式网站。

PS:原谅我又么有读懂题意---!

/**
 * Return an array of size *returnSize.
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* plusOne(int* digits, int digitsSize, int* returnSize) {
    int i;
    int index=0;
    int carry=0;
    int flag=1;
    //感觉是偷了个懒,只有全是9的时候才进1.。。。。。所以
    //只有个位数加1,不是每个都加,所以用flag
    for(i=digitsSize-1;i>=0;i--){
        //不是每个都加1
        if(digits[i]+flag+index>9){
            digits[i]=0;
            index=1;
            if(i==0){
                carry=1;
            }
        }else{
            digits[i]=digits[i]+1;
            break;
        }
        flag=0;
        // printf("%d",digits[i]);
    }    
    //这个值也得写明白,不然程序不知道???
    *returnSize=digitsSize+carry;

    if(carry){
        int *newdigits=(int*)malloc(sizeof(int)*digitsSize+1);
        newdigits[0]=1;
        for(i=1;i

PS:迷迷糊糊就写完了。。。。。不执行。。。。。看了一下网上的程序貌似最后还要返回那个returnSize。。。。。。。。。。。。。。这才可以。

其实只有全是9的时候才会产生首位进位。。。。。。。。。。。


当前文章:LeetCode066PlusOneC语言
网站链接:http://www.wjwzjz.com/article/pidsic.html
在线咨询
服务热线
服务热线:028-86922220
TOP