您当前的位置: 首页 >  leetcode

不脱发的程序猿

暂无认证

  • 2浏览

    0关注

    492博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

力扣(LeetCode)刷题,简单题(第9期)

不脱发的程序猿 发布时间:2020-04-02 21:14:30 ,浏览量:2

目录

第1题:反转图像

第2题:上升下降字符串

第3题:合并两个排序链表

第4题:和为0的N个唯一整数

第5题:反转二叉树

第6题:高度检查器

第7题:距离值

第8题:自除数

第9题:最大数值

第10题:反转字符串

力扣(LeetCode)定期刷题,每期10道题,业务繁重的同志可以看看我分享的思路,不是最高效解决方案,只求互相提升。

第1题:反转图像

试题要求如下:

回答(C语言):

/**
 * Return an array of arrays of size *returnSize.
 * The sizes of the arrays are returned as *returnColumnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */
int** flipAndInvertImage(int** A, int ASize, int* AColSize, int* returnSize, int** returnColumnSizes){
    int num=0,data_buf=0;

    for(int i=0;inext);
        return l2;
    }
    else
    {
        l1->next=mergeTwoLists(l1->next, l2);
        return l1;
    }
}

运行效率如下所示:

第4题:和为0的N个唯一整数

试题要求如下:

回答(C语言):

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* sumZero(int n, int* returnSize){
    int* data_buf=(int*)malloc(sizeof(int)*(n));
    memset(data_buf,0,sizeof(int)*(n));
    *returnSize=n;

    for(int i=0,j=n-1;ileft;
    root->left=invertTree(root->right);
    root->right=invertTree(tmp);
    return root;
}

运行效率如下所示:

第6题:高度检查器

试题要求如下:

回答(C语言):

int cmp(const void *a, const void *b)
{
    return *(int*)a - *(int*)b;
}
int heightChecker(int* heights, int heightsSize){
    int ans = 0, tmp[heightsSize];
    
    for(int i = 0;i < heightsSize;++i)
        tmp[i] = heights[i];

    qsort(tmp, heightsSize, sizeof(int), cmp);

    for(i = 0;i < heightsSize;++i)
    {
        if(tmp[i] != heights[i])
            ++ans;
    }
    return ans;
}

运行效率如下所示:

第7题:距离值

试题要求如下:

回答(C语言):

int findTheDistanceValue(int* arr1, int arr1Size, int* arr2, int arr2Size, int d){
    int ans = 0;

    for(int i = 0,j = 0;i < arr1Size;++i)
    {
        for(j = 0;j < arr2Size && abs(arr1[i] - arr2[j]) > d;++j);
        if(j >= arr2Size)
            ++ans;
    }

    return ans;
}

运行效率如下所示:

第8题:自除数

试题要求如下:

回答(C语言):

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* selfDividingNumbers(int left, int right, int* returnSize){

    int len = right-left+1;
    int index=0;
    int *returnNum = (int *)malloc(sizeof(int)*(len+1));
    memset(returnNum, 0, sizeof(int)*(len+1));

    for(int i=left; i 0){
            num = tmp%10;
            if(num == 0 || i%num != 0){
                flag = 1;
            }
            tmp /= 10;
        }
        if(flag == 0)
            returnNum[index++] = i;
    }

    *returnSize = index;
    return returnNum;
}

运行效率如下所示:

第9题:最大数值

试题要求如下:

回答(C语言):

/* a-b, k等于结果的符号位    */
/* k=1, b>a, 返回b*1 + a*0  */
/* k=0, b> 63) & 1;
    return b * k + a * (k ^ 1);
}

运行效率如下所示:

第10题:反转字符串

试题要求如下:

回答(C语言):

void reverseString(char* s, int sSize){
    int i=0,j=sSize-1;
    char data_buf;
    
    while(i            
关注
打赏
1664101891
查看更多评论
0.0464s