【九日集训】《LeetCode刷题报告》题解内容 Ⅳ

⛳「write in front」⛳

🔎大家好,我是謓泽,希望你看完之后,能对你有所帮助,不足请指正!共同学习交流🔎
🏅2021年度博客之星物联网与嵌入式开发TOP5~2021博客之星Top100~阿里云专家博主 & 星级博主~掘金⇿InfoQ创作者~周榜163﹣总榜1479⇿全网访问量30w+🏅
🆔本文由 謓泽 原创 CSDN首发🙉如需转载还请通知
📝个人主页⇢打打酱油desuCSDN博客💬
🎁欢迎各位⇢点赞👍 + 收藏⭐️ + 留言📝​
📣系列专栏⇢
九日集训之力扣(LeetCode)算法[₀~¹]🎓
✉️我们并非登上我们所选择的舞台,演出并非我们所选择的剧本📩

🎃本章博客题目力扣链接 

  1. 1470. 重新排列数组 - 力扣(LeetCode)
  2. 1929. 数组串联 - 力扣(LeetCode)
  3. 1920. 基于排列构建数组 - 力扣(LeetCode)
  4. 1480. 一维数组的动态和 - 力扣(LeetCode)
  5. 剑指 Offer 58 - II. 左旋转字符串 - 力扣(LeetCode)

 🌠很久以前的文章了,还是去年跟着英雄哥刷题在社区每日打卡的的时候( •̀ .̫ •́ )✧ 

㈠重新排列数组 

题目

示例

提示👇

  • 1 <= n <= 500
  • nums.length == 2n
  • 1 <= nums[i] <= 10^

📝题解思路

首先,写这个代码必须要知道 malloc() 函数的作用。

  • malloc 时动态内存分配函数,用于申请一块连续的指定大小的内存块区域以void*类型返回分配的内存区域地址。
  • 在使用 malloc 开辟空间时,使用完成一定要释放空间,如果不释放会造内存泄漏。
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* shuffle(int* nums, int numsSize, int n, int* returnSize)
{
    int i,j = 0;
    int *ret = (int *)malloc( sizeof(int) * numsSize ); 
    for(i = 0; i < n; ++i) 
    {
        ret[j++] = nums[i];
        ret[j++] = nums[n+i];
    }
    *returnSize = numsSize;
    return ret;
}

㈡数组串联

题目

 示例

提示👇

  • n == nums.length
  • 1 <= n <= 1000
  • 1 <= nums[i] <= 1000 

📝题解思路

  •  这题和上面是很类似的,不同之处就是其申请长度:2 * numsSize 的地址。
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* getConcatenation(int* nums, int numsSize, int* returnSize)
{
    int i,j = 0;
    int *ret = (int *)malloc( sizeof(int) * 2 *numsSize ); 
    for(i = 0; i < numsSize; ++i) 
    {
        ret[i] = nums[i];
        ret[numsSize+i] = nums[i];
    }
    *returnSize = 2 * numsSize;
    return ret;
}

㈢基于排列构建数组

题目示例提示

  • 1 <= nums.length <= 1000
  • 0 <= nums[i] < nums.length
  • nums 中的元素 互不相同
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* buildArray(int* nums, int numsSize, int* returnSize)
{
    int i;
    int *ans = (int *)malloc( sizeof(int) * numsSize );
    for(i = 0;i<numsSize;i++)
    {
        ans[i] = nums[nums[i]];
    }
    *returnSize = numsSize;
    return ans;
}

㈣一维数组动态和

题目

示例

  提示:

  • 1 <= nums.length <= 1000
  • -10^6 <= nums[i] <= 10^6
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* runningSum(int* nums, int numsSize, int* returnSize)
{
    int i,j,k = 0;
    int *ret = (int *)malloc( sizeof(int) * numsSize );
    for(i = 0;i < numsSize;i++)
    {
        //ret[i] = nums[i];
        k = 0;
        for(j = 0;j<=i;j++)
        {
            k+=nums[j];
        }
        ret[i] = k;
    }
    *returnSize = numsSize;
    return ret;
}

㈤左旋转字符串

题目

 示例

提示

  • 1 <= k < s.length <= 10000
char* reverseLeftWords(char* s, int n)
{
    int i;
    int k = strlen(s);
    char* ret = (char *)malloc( k+1 );
    for(i=0;i<k;i++)
    {
        ret[i] = s[(i+n)%k];
    }
    ret[k] = '\0';
    return ret;
}

评论 38
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

謓泽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值