当前位置:网站首页>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;
}
边栏推荐
- MySQL啟動報錯“Bind on TCP/IP port: Address already in use”
- 【分享】企业如何进行施行规划?
- 自动化运维文章大合集
- Award winning survey streamnational sponsored 2022 Apache pulsar user questionnaire
- Augmented reality experiment IV of Shandong University
- kubelet Error getting node 问题求助
- 矩阵求逆操作的复杂度分析(逆矩阵的复杂度分析)
- 844. 比较含退格的字符串
- 19. 删除链表的倒数第 N 个结点
- Do you know these advantages of ERP system?
猜你喜欢

openstack详解(二十四)——Neutron服务注册

Redis source code analysis hash object (z\u hash)

Complexity analysis of matrix inversion operation (complexity analysis of inverse matrix)

山东大学增强现实实验四

Typescript high level feature 1 - merge type (&)

ArcGIS 10.9.1 地质、气象体元数据处理及服务发布调用

Award winning survey streamnational sponsored 2022 Apache pulsar user questionnaire

Type-C扩展坞自适应供电专利维权案例

山东大学项目实训(四)—— 微信小程序扫描web端二维码实现web端登录

C language printing diamond
随机推荐
Pulsar job Plaza | Tencent, Huawei cloud, shrimp skin, Zhong'an insurance, streamnational and other hot jobs
基于SIC32F911RET6设计的腕式血压计方案
Fabric.js 動態設置字號大小
1721. 交换链表中的节点
206. 反转链表
19. delete the penultimate node of the linked list
Sword finger offer 06 Print linked list from end to end
CUMT学习日记——ucosII理论解析—任哲版教材
[C language - Advanced pointer] mining deeper knowledge of pointer
报错RuntimeError: BlobReader error: The version of imported blob doesn‘t match graph_transformer
远程办公最佳实践及策略
面试题 17.10. 主要元素
ERP体系的这些优势,你知道吗?
Strength and appearance Coexist -- an exclusive interview with Liu Yu, a member of Apache pulsar PMC
【软件】大企业ERP选型的方法
20. 有效的括号
C language printing diamond
[software] ERP model selection method for large enterprises
Erreur de démarrage MySQL "BIND on TCP / IP Port: Address already in use"
ArcGIS 10.9.1 geological and meteorological volume metadata processing and service publishing and calling