• 【九日集训】《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 开辟空间时,使用完成一定要释放空间,如果不释放会造内存泄漏。
    1. /**
    2. * Note: The returned array must be malloced, assume caller calls free().
    3. */
    4. int* shuffle(int* nums, int numsSize, int n, int* returnSize)
    5. {
    6. int i,j = 0;
    7. int *ret = (int *)malloc( sizeof(int) * numsSize );
    8. for(i = 0; i < n; ++i)
    9. {
    10. ret[j++] = nums[i];
    11. ret[j++] = nums[n+i];
    12. }
    13. *returnSize = numsSize;
    14. return ret;
    15. }

    ㈡数组串联

    题目

     示例

    提示👇

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

    📝题解思路

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

    ㈢基于排列构建数组

    题目示例提示

    • 1 <= nums.length <= 1000
    • 0 <= nums[i] < nums.length
    • nums 中的元素 互不相同
    1. /**
    2. * Note: The returned array must be malloced, assume caller calls free().
    3. */
    4. int* buildArray(int* nums, int numsSize, int* returnSize)
    5. {
    6. int i;
    7. int *ans = (int *)malloc( sizeof(int) * numsSize );
    8. for(i = 0;i
    9. {
    10. ans[i] = nums[nums[i]];
    11. }
    12. *returnSize = numsSize;
    13. return ans;
    14. }

    ㈣一维数组动态和

    题目

    示例

      提示:

    • 1 <= nums.length <= 1000
    • -10^6 <= nums[i] <= 10^6
    1. /**
    2. * Note: The returned array must be malloced, assume caller calls free().
    3. */
    4. int* runningSum(int* nums, int numsSize, int* returnSize)
    5. {
    6. int i,j,k = 0;
    7. int *ret = (int *)malloc( sizeof(int) * numsSize );
    8. for(i = 0;i < numsSize;i++)
    9. {
    10. //ret[i] = nums[i];
    11. k = 0;
    12. for(j = 0;j<=i;j++)
    13. {
    14. k+=nums[j];
    15. }
    16. ret[i] = k;
    17. }
    18. *returnSize = numsSize;
    19. return ret;
    20. }

    ㈤左旋转字符串

    题目

     示例

    提示

    • 1 <= k < s.length <= 10000
    1. char* reverseLeftWords(char* s, int n)
    2. {
    3. int i;
    4. int k = strlen(s);
    5. char* ret = (char *)malloc( k+1 );
    6. for(i=0;i
    7. {
    8. ret[i] = s[(i+n)%k];
    9. }
    10. ret[k] = '\0';
    11. return ret;
    12. }

  • 相关阅读:
    ELK介绍
    语义分割在线标注思路
    github 中的java前后端项目整合到本地运行
    Linux —— 进程控制
    【AI语言模型】阿里推出音视频转文字引擎
    【数据结构】树与二叉树(八):二叉树的中序遍历(非递归算法NIO)
    行为型模式-备忘录模式
    《七月集训》(第六天)——滑动窗口
    系统设计 - 我们如何通俗的理解那些技术的运行原理 - 第一部分:通信协议(1)
    C++入门——域作用符,命名空间的讲解
  • 原文地址:https://blog.csdn.net/weixin_52632755/article/details/121913252