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

目录

第1题:存在重复元素

第2题:2的幂

第3题:移动零

第4题:缺失数字

第5题:第一个错误的版本

第6题:按摩师

第7题:3的幂

第8题:求1+2+...+n

第9题:数值变为0的步数

第10题:有多少小于当前数字的数


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

第1题:存在重复元素

试题要求如下:

回答(C语言):

int compare(const void *a, const void *b){
    return *(int*)a - *(int*)b;
}

bool containsDuplicate(int* nums, int numsSize){
    qsort(nums, numsSize, sizeof(int), compare);
    for(int i = 0; i < numsSize - 1; i++){
        if(nums[i] == nums[i+1]){
            return true;
        }
    }
    return false;
}

运行效率如下所示:


第2题:2的幂

试题要求如下:

回答(C语言):

bool isPowerOfTwo(int n){
    while(n%2==0&&n>1){
        n=n/2;
    }

    if(n==1){
        return true;
    }
    else{
        return false;
    }
}

运行效率如下所示:


第3题:移动零

试题要求如下:

回答(C语言):

void moveZeroes(int* nums, int numsSize){

    for(int i=0,j = 0,temp=0; j < numsSize;++j) {
        if(nums[j] != 0) {
            temp = nums[i];
            nums[i] = nums[j];
            nums[j] = temp;
            ++i;
        }
    }
}

运行效率如下所示:


第4题:缺失数字

试题要求如下:

回答(C语言):

int missingNumber(int* nums, int numsSize){
    int ans = numsSize*(numsSize + 1)/2, i;

    for(i = 0; i < numsSize; i++){
        ans -= nums[i];
    }
    return ans;
}

运行效率如下所示:


第5题:第一个错误的版本

试题要求如下:

回答(C语言):

// Forward declaration of isBadVersion API.
bool isBadVersion(int version);

int firstBadVersion(int n) {
    long low=1;
    long high=n;
    
    while(low<high){
        long mid=(low+high)/2;
        if(!isBadVersion(mid)){
            low=mid+1;
        }
        else{
            high=mid;
        }
    }
    return low;
}

运行效率如下所示:


第6题:按摩师

试题要求如下:

回答(C语言):

//设dp[i]表示第i个房屋之前的最高金额,则求dp[numsSize-1]即可。
//状态转移方程:

//dp[0] = nums[0]
//dp[1] = max(nums[1], nums[0])
//dp[i] = max(dp[i-2] + nums[i], dp[i-1])(i>=2)

int massage(int* nums, int numsSize){
    if(!nums || numsSize == 0)
        return 0;

    if(numsSize == 1)
        return nums[0];

    int *dp = (int*)malloc(sizeof(int) * numsSize);
    dp[0] = nums[0];
    dp[1] = fmax(nums[1], nums[0]);

    for(int i = 2; i < numsSize; i++){
        dp[i] = fmax(dp[i-2] + nums[i], dp[i-1]);
    }
    
    return dp[numsSize - 1];
}

运行效率如下所示:


第7题:3的幂

试题要求如下:

回答(C语言):

bool isPowerOfThree(int n){
    if (n == 0) 
        return false;

    while(n%3==0){
        n/=3;
    }

    if(n==1)
        return true;
    
    return false;
}

运行效率如下所示:


第8题:求1+2+...+n

试题要求如下:

回答(C语言):

//(1)如果多个变量均非0(包括None、False等),那么返回最后一个变量的值。如3 and 2 and 'a'的返回值为'a';
//(2)如果多个变量中存在0值,则返回第一个0值。如1 and 'a' and 0 and None的返回值为0。


int sumNums(int n){
    int res = n;
    n && (res += sumNums(n-1));
    return res;
}

运行效率如下所示:


第9题:数值变为0的步数

试题要求如下:

回答(C语言):

int numberOfSteps (int num){
    int cou=0;
    
    while(num!=0){
        if(num%2==0){
            num/=2;
        }
        else{
            num-=1;
        }
        cou++;
    }

    return cou;
}

运行效率如下所示:


第10题:有多少小于当前数字的数

试题要求如下:

回答(C语言):

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* smallerNumbersThanCurrent(int* nums, int numsSize, int* returnSize){
    int* data_buf=(int*)malloc(sizeof(int)*(numsSize+1));
    memset(data_buf,'\0',sizeof(int)*(numsSize+1));

    for(int i=0;i<numsSize;i++){
        for(int j=0;j<numsSize;j++){
            if(nums[j]<nums[i])
                data_buf[i]++;
        }
    }

    data_buf[numsSize]='\0';
    *returnSize=numsSize;

    return data_buf;
}

运行效率如下所示:

发布了170 篇原创文章 · 获赞 8870 · 访问量 100万+

猜你喜欢

转载自blog.csdn.net/m0_38106923/article/details/104955241
今日推荐