题目![](https://img-blog.csdnimg.cn/9818afcb2ced4a48af95c7e48f87a47a.png)
示例![](https://img-blog.csdnimg.cn/29f330a134cd47e598079a11369a2ce6.png)
思路
解题思路
构造一个数组,按照题目要求遍历枚举数组元素重新构造即可
遍历数组可以使用双指针,一个指向x下标,一个指向y下标,一次遍历即可构造完成
代码
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* shuffle(int* nums, int numsSize, int n, int* returnSize){
int * ans = (int *)malloc(sizeof(int) * numsSize);
*returnSize = 0;
for(int x = 0, y = n; x < n; x++, y++)
{
ans[(*returnSize)++] = nums[x];//构造x
ans[(*returnSize)++] = nums[y];//构造y
}
return ans;
}
作者:xun-ge-v
链接:https://leetcode.cn/problems/shuffle-the-array/solution/-by-xun-ge-v-y785/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。