当前位置:首页 » 《随便一记》 » 正文

[小玄的刷题日记]《LeetCode零基础指南》(第5讲) 指针_forever_bryant的博客

5 人参与  2022年04月16日 08:53  分类 : 《随便一记》  评论

点击全文阅读


1470. 重新排列数组 - 力扣(LeetCode) (leetcode-cn.com)

int* shuffle(int* nums, int numsSize, int n, int* returnSize){
    int i = 0;
    int* ret = (int*)malloc(sizeof(int) * numsSize);
    for(i = 0;i < numsSize;i++)
    {
        if(i&1)
        ret[i] = nums[n + i / 2];
        else
        ret[i] = nums[(i + 1)/2];
    }
    *returnSize = numsSize;
    return ret;
}

 1929. 数组串联 - 力扣(LeetCode) (leetcode-cn.com)

int* getConcatenation(int* nums, int numsSize, int* returnSize){
    int i = 0;
    int* ret = (int*)malloc(sizeof(int) * numsSize * 2);
    for(i = 0;i < numsSize;i++)
    {
        ret[i +numsSize] = ret[i] = nums[i];
    }
    *returnSize = 2 * numsSize;
    return ret;
}

​​​​​1920. 基于排列构建数组 - 力扣(LeetCode) (leetcode-cn.com)​​​​​1920. 基于排列构建数组 - 力扣(LeetCode) (leetcode-cn.com)

int* buildArray(int* nums, int numsSize, int* returnSize){
    int i = 0;
    int* ret = (int*)malloc(sizeof(int) * numsSize);
    for(i = 0;i < numsSize;i++)
    {
        ret[i] = nums[nums[i]];
    }
    *returnSize = numsSize;
    return ret;
}

1480. 一维数组的动态和 - 力扣(LeetCode) (leetcode-cn.com)1480. 一维数组的动态和 - 力扣(LeetCode) (leetcode-cn.com)

int* runningSum(int* nums, int numsSize, int* returnSize){
    int i = 0;
    int* ret = (int*)malloc(sizeof(int) * numsSize);
    for(i = 0;i < numsSize;i++)
    {
        int j = i;
        int sum = 0;
        for(j = 0;j <= i;j++)
        {
            sum += nums[j];
        }
        ret[i] = sum;
    }
    *returnSize = numsSize;
    return ret;
}

剑指 Offer 58 - II. 左旋转字符串 - 力扣(LeetCode) (leetcode-cn.com)

char* reverseLeftWords(char* s, int k){
    int i;
    int n = strlen(s);
    char *ret = (char *)malloc( (n + 1) * sizeof(char) );    // (1)
    for(i = 0; i < n; ++i) {
        ret[i] = s[(i + k) % n];                             // (2)
    }
    ret[n] = '\0';                                           // (3)
    return ret;
}

1108. IP 地址无效化 - 力扣(LeetCode) (leetcode-cn.com)

char * defangIPaddr(char * address){
    char* ret = (char*)malloc(1000 * sizeof(char));
    int returnSize = 0;
    int i = 0;
    for(i = 0;address[i];i++)
    {
        if(address[i] == '.')
        {
            ret[ returnSize++] = '[';
            ret[ returnSize++] = '.';
            ret[ returnSize++] = ']';
        }
        else
        ret[returnSize++] = address[i];
    }
    ret[returnSize] = '\0';
    return ret;
}

Loading Question... - 力扣(LeetCode) (leetcode-cn.com)

char* replaceSpace(char* s){
    char* ret = (char*)malloc(300001 * sizeof(char));
    int returnSize = 0;
    int i = 0;
    for(i = 0;s[i];i++)
    {
        if(s[i] == ' ')
        {
            ret[returnSize ++] = '%%';
            ret[returnSize ++] = '2';
            ret[returnSize ++] = '0';
        }
        else
        ret[returnSize ++] = s[i];
    }
    ret[ returnSize] ='\0';
    return ret;
}

1365. 有多少小于当前数字的数字 - 力扣(LeetCode) (leetcode-cn.com)

int* smallerNumbersThanCurrent(int* nums, int numsSize, int* returnSize){
    int i = 0;
    int* ret = (int*)malloc(numsSize * sizeof(int));
    for(i = 0;i < numsSize;i++)
    {
        int count = 0;
        for(int j = 0;j < numsSize;j++)
        {
            if(nums[i] > nums[j])
                count++;
        }
        ret[i] = count;
    }
    *returnSize = numsSize;
    return ret;
}

1389. 按既定顺序创建目标数组 - 力扣(LeetCode) (leetcode-cn.com)

int* createTargetArray(int* nums, int numsSize, int* index, int indexSize, int* returnSize){
    int len = 0;
    int i,j,ins,idx;
    int* ret = (int*)malloc(sizeof(int) * numsSize);
    for(i = 0;i < numsSize;i++)
    {
        idx = index[i];
        ins = nums[i];
        for(j = len;j > idx;--j)
        {
            ret[j] = ret[j - 1];
        }
        ret[idx] = ins;
        ++len;
    }
    *returnSize = len;
    return ret;
}

 

 


点击全文阅读


本文链接:http://m.zhangshiyu.com/post/38145.html

数组  排列  构建  
<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1