当前位置:网站首页>2161. divide the array according to the given number

2161. divide the array according to the given number

2022-06-11 09:14:00 Mr Gao

2161. Divide the array according to the given number

I'll give you a subscript from 0 The starting array of integers nums And an integer pivot . Would you please nums Rearrange , Make the following conditions true :

 All less than  pivot  All elements greater than  pivot  The elements of   Before  .
 All equal to  pivot  All elements appear in less than and greater than  pivot  The elements of   middle  .
 Less than  pivot  Between elements of and greater than  pivot  Between elements of   Relative order   No change .
     More formal , Consider each pair  pi,pj ,pi  Is the initial position  i  The new location of the element ,pj  Is the initial position  j  The new location of the element . For less than  pivot  The elements of , If  i < j  And  nums[i] < pivot  and  nums[j] < pivot  All set up , that  pi < pj  It was also established . Allied , For greater than  pivot  The elements of , If  i < j  And  nums[i] > pivot  and  nums[j] > pivot  All set up , that  pi < pj .

Please go back and rearrange nums The result array after the array .

Example 1:

Input :nums = [9,12,5,10,14,3,10], pivot = 10
Output :[9,5,3,10,10,12,14]
explain :
Elements 9 ,5 and 3 Less than pivot , So they're on the far left of the array .
Elements 12 and 14 Greater than pivot , So they're on the far right of the array .
Less than pivot The relative position sum of the elements of is greater than pivot The relative positions of the elements are [9, 5, 3] and [12, 14] , Their relative order in the result array needs to be preserved .

Example 2:

Input :nums = [-3,4,3,2], pivot = 2
Output :[-3,2,4,3]
explain :
Elements -3 Less than pivot , So on the far left of the array .
Elements 4 and 3 Greater than pivot , So they're on the far right of the array .
Less than pivot The relative position sum of the elements of is greater than pivot The relative positions of the elements are [-3] and [4, 3] , Their relative order in the result array needs to be preserved .

This question , We can set up a 2*numsize Array of , It can solve the problem well without changing the position
The solution code is as follows :

/** * Note: The returned array must be malloced, assume caller calls free(). */
int* pivotArray(int* nums, int numsSize, int pivot, int* returnSize){
    
    int *numst=(int *)malloc(sizeof(int)*numsSize*2);
    int epo=numsSize;
    int max=numsSize*2-1;
    int i=0;
    for(i;i<numsSize*2;i++){
    
        if(i<numsSize){
    
              numst[i]=nums[i];
        }
        else{
    
            numst[i]=-59789;
        }
    }
    for(i=numsSize-1;i>=0;i--){
    
        if(nums[i]>pivot){
    
            numst[max--]=nums[i];
            numst[i]=-59789;

        }
        if(nums[i]==pivot){
    
             numst[epo++]=nums[i];
             numst[i]=-59789;
        }
    }
    int po=0;
    for(i=0;i<numsSize*2;i++){
    
        if(numst[i]!=-59789){
    
    numst[po++]=numst[i];

        }
    }
    *returnSize=numsSize;
    return numst;
}
原网站

版权声明
本文为[Mr Gao]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206110906263749.html