当前位置:网站首页>Leetcode- sort arrays by parity

Leetcode- sort arrays by parity

2022-06-13 08:05:00 Sharp soldier fruit

Give you an array of integers nums, take nums Move all even elements in the array to the front of the array , Followed by all odd elements . Returns... That meets this condition Any array As the answer .

 Example  1:

 Input :nums = [3,1,2,4]
 Output :[2,4,3,1]
 explain :[4,2,3,1][2,4,1,3]  and  [4,2,1,3]  It will also be seen as the right answer .

source : Power button (LeetCode)

Analysis of the title shows that

  1. Traversal array , Take out even numbers , Insert header data into result array
  2. Take out the even tail and insert data into the result array

So use JS It's easy to write

/** * @param {number[]} nums * @return {number[]} * @ give an example  nums = [3,1,2,4],sortArrayByParity(nums)=[4,2,3,1] */
var sortArrayByParity = function(nums) {
    
    let res=[];
    nums.map((item)=>{
    
        if(item%2==0)
        {
    
            res.unshift(item);
        }
        else{
    
            res.push(item);
        }
    })

    return res;
};

Two methods

unshift

Insert data in the head , On the contrary ,shift The header is deleted

push

Insert data at the end , It's also related to unshift contrary

that C Language can use the same idea , It's actually a double pointer , One points to the beginning , One points to the end

/** * Note: The returned array must be malloced, assume caller calls free(). */
int* sortArrayByParity(int* nums, int numsSize, int* returnSize){
    
    int *returnNums = (int *)malloc(sizeof(int) * numsSize);
    int *left=returnNums,*right=returnNums+numsSize-1;
    int index=0;
    while(index<numsSize)
    {
    
        if(nums[index]%2==0)
        {
    
            *left=nums[index];
            left++;
        }
        else{
    
            *right=nums[index];
            right--;
        }
        index++;
    }

    *returnSize=numsSize;

    return returnNums;
}

Advanced

Sort the array , For convenience nums[i] In an odd number of ,i It's also Odd number ; When nums[i] For even when , i It's also even numbers .

 Input :nums = [4,2,5,7]
 Output :[4,5,2,7]
 explain :[4,7,2,5],[2,5,4,7],[2,7,4,5]  It will also be accepted .

You can get by the question

  1. First judge whether it is odd or even
  2. Odd number , The subscript is odd , That is from 1 Start , Every time 2
  3. even numbers , The following table shows even numbers , from 0 Start , Every time 2

C Language

/** * Note: The returned array must be malloced, assume caller calls free(). */
int* sortArrayByParityII(int* nums, int numsSize, int* returnSize){
    
    int *returnNums = (int *)malloc(sizeof(int) * numsSize);
    int even=0,odd=1;
    int index=0;
    while(index<numsSize)
    {
    
        if(nums[index]%2==0)
        {
    
            returnNums[even]=nums[index];
            even+=2;
        }
        else{
    
            returnNums[odd]=nums[index];
            odd+=2;
        }
        index++;
    }

    *returnSize=numsSize;

    return returnNums;
}
原网站

版权声明
本文为[Sharp soldier fruit]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/164/202206130802194069.html