Skip to content

Latest commit

 

History

History
65 lines (49 loc) · 1.44 KB

_1470. Shuffle the Array.md

File metadata and controls

65 lines (49 loc) · 1.44 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 03, 2024

Last updated : July 01, 2024


Related Topics : Array

Acceptance Rate : 88.71 %


Solutions

C

// I presume this is much faster cause there's no multiplication anymore

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* shuffle(int* nums, int numsSize, int n, int* returnSize){
    int* output = (int*) malloc(sizeof(int) * numsSize);
    *returnSize = numsSize;
    
    int pt1 = 0;
    int pt2 = n;
    for (int i = 0; i < numsSize; i += 2, pt1++, pt2++) {
        output[i] = nums[pt1];
        output[i + 1] = nums[pt2];
    }

    return output;
}
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* shuffle(int* nums, int numsSize, int n, int* returnSize){
    int* output = (int*) malloc(sizeof(int) * numsSize);
    *returnSize = numsSize;

    for (int i = 0; i < n; i++) {
        output[2 * i] = nums[i];
        output[2 * i + 1] = nums[i + n];
    }

    return output;
}