力扣刷题每日打卡

Posted 布小禅

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了力扣刷题每日打卡相关的知识,希望对你有一定的参考价值。


力扣刷题:

  1. 重新排列数组:
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* shuffle(int* nums, int numsSize, int n, int* returnSize)
    if (numsSize==1) 
    
        *returnSize = 1;
        return nums;
    
    int *res = (int *)malloc(sizeof(int) * numsSize);
    int i=0;
    int j=0;
    while (j!=numsSize)
    
        res[j] = nums[i];
        res[j+1] = nums[i+(numsSize/2)];
        i++;
        j+=2;
    
    *returnSize = numsSize;
    return res;

  1. 数组串联:
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* getConcatenation(int* nums, int numsSize, int* returnSize)
    int *res = (int *)malloc(sizeof(int) * (numsSize * 2));
    for (int i=0;i<numsSize;i++)
    
        res[i] = nums[i];
        res[i+numsSize] = nums[i];
    
    *returnSize = numsSize * 2;
    return res;

  1. 基于排列构建数组:
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* buildArray(int* nums, int numsSize, int* returnSize)
    int *res = (int *)malloc(sizeof(int) * numsSize);
    for (int i=0;i<numsSize;i++)
    
        res[i] = nums[nums[i]];
    
    *returnSize = numsSize;
    return res;

  1. 一维数组的动态和:
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* runningSum(int* nums, int numsSize, int* returnSize)
    if (numsSize == 1)
        *returnSize = 1;
        return nums;
    
    int *res = (int *)malloc(sizeof(int) * numsSize);
    for (int i=0;i<numsSize;i++)
    
        res[i] = sumNum(i, nums);
    
    *returnSize = numsSize;
    return res;

int sumNum(int n, int *nums)

    int sum = 0;
    for(int i=0;i<=n;i++)
    
        sum += nums[i];
    
    return sum;

剑指 Offer 58 - II. 左旋转字符串

char* reverseLeftWords(char* s, int n)
    int len = strlen(s);
    char *res = (char *)malloc(sizeof(char)*(len+1));
    for (int i=0;i<len;++i)
    
        res[i] = s[(i+n)%len];
    
    res[len] = '\\0';
    return res;

  1. IP 地址无效化
char * defangIPaddr(char * address)
    char* res = (char *)malloc(sizeof(char) * 30);
    int returnSize = 0;
    for (int i=0;address[i];++i)
    
        if (address[i] == '.')
        
            res[returnSize++] = '[';
            res[returnSize++] = '.';
            res[returnSize++] = ']';
        else
            res[returnSize++] = address[i]; 
        
    
    res[returnSize] = '\\0';
    return res;

剑指 Offer 05. 替换空格

char* replaceSpace(char* s)
    char *ans = (char *)malloc(sizeof(char) * strlen(s) * 3 + 1);
    int i = 0;
    while (*s) 
        if (*s == ' ') 
            ans[i++] = '%';
            ans[i++] = '2';
            ans[i++] = '0';
         else 
            ans[i++] = *s;
        
        s++;
    
    ans[i] = 0;
    return ans;

  1. 有多少小于当前数字的数字
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* smallerNumbersThanCurrent(int* nums, int numsSize, int* returnSize)
    int *res = (int *)malloc(sizeof(int) * numsSize);
    for (int i=0;i<numsSize;i++)
    
        res[i] = minSum(nums[i], nums, numsSize);
    
    *returnSize = numsSize;
    return res;

int minSum(int n, int *nums, int numsSize)

    int res = 0;
    for (int i=0;i<numsSize;i++)
    
        if (nums[i] < n) res++;
    
    return res;

剑指 Offer 17. 打印从1到最大的n位数

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* printNumbers(int n, int* returnSize)
    int  max = powSum(10, n) - 1;
    int *res = (int *)malloc(sizeof(int) * max);
    for (int i=0;i<max;i++)
    
        res[i] = i+1;
    
    *returnSize = max;
    return res;

int powSum(int x, int y)

    int res = 1;
    for (int i=0;i<y;i++)
    
        res *= x;
    
    return res;

  1. 按既定顺序创建目标数组
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* createTargetArray(int* nums, int numsSize, int* index, int indexSize, int* returnSize)
    int len = 0;
    int *res = (int *)malloc(sizeof(int) * numsSize);
    for (int i=0;i<numsSize;++i)
    
        for (int j=len;j>index[i];--j)
        
            /*插入操作,也可以单独封装函数*/
            res[j] = res[j-1];
        
        res[index[i]] = nums[i];
        ++len;
    
    *returnSize = len;
    return res;

以上是关于力扣刷题每日打卡的主要内容,如果未能解决你的问题,请参考以下文章

力扣刷题每日打卡

开启力扣刷题之路 >数组> 二分法

力扣刷题感受

力扣刷题。

力扣刷题算法笔记(javascript版)下

力扣刷题资源