⛳「write in front」⛳ 🔎大家好,我是謓泽,希望你看完之后,能对你有所帮助,不足请指正!共同学习交流🔎 🏅2021年度博客之星物联网与嵌入式开发TOP5~2021博客之星Top100~阿里云专家博主 & 星级博主~掘金⇿InfoQ创作者~周榜163﹣总榜1479⇿全网访问量30w+🏅 🆔本文由 謓泽 原创 CSDN首发🙉如需转载还请通知⚠ 📝个人主页⇢打打酱油desuCSDN博客💬 🎁欢迎各位⇢点赞👍 + 收藏⭐️ + 留言📝 📣系列专栏⇢九日集训之力扣(LeetCode)算法[₀~¹]🎓 ✉️我们并非登上我们所选择的舞台,演出并非我们所选择的剧本📩
🎃本章博客题目力扣链接
🌠很久以前的文章了,还是去年跟着英雄哥刷题在社区每日打卡的的时候( •̀ .̫ •́ )✧
题目
示例
提示👇
1 <= n <= 500
nums.length == 2n
1 <= nums[i] <= 10^
📝题解思路
首先,写这个代码必须要知道 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
📝题解思路
/**
* 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;
}