当前位置:网站首页>2161. 根据给定数字划分数组
2161. 根据给定数字划分数组
2022-06-11 09:06:00 【Mr Gao】
2161. 根据给定数字划分数组
给你一个下标从 0 开始的整数数组 nums 和一个整数 pivot 。请你将 nums 重新排列,使得以下条件均成立:
所有小于 pivot 的元素都出现在所有大于 pivot 的元素 之前 。
所有等于 pivot 的元素都出现在小于和大于 pivot 的元素 中间 。
小于 pivot 的元素之间和大于 pivot 的元素之间的 相对顺序 不发生改变。
更正式的,考虑每一对 pi,pj ,pi 是初始时位置 i 元素的新位置,pj 是初始时位置 j 元素的新位置。对于小于 pivot 的元素,如果 i < j 且 nums[i] < pivot 和 nums[j] < pivot 都成立,那么 pi < pj 也成立。类似的,对于大于 pivot 的元素,如果 i < j 且 nums[i] > pivot 和 nums[j] > pivot 都成立,那么 pi < pj 。
请你返回重新排列 nums 数组后的结果数组。
示例 1:
输入:nums = [9,12,5,10,14,3,10], pivot = 10
输出:[9,5,3,10,10,12,14]
解释:
元素 9 ,5 和 3 小于 pivot ,所以它们在数组的最左边。
元素 12 和 14 大于 pivot ,所以它们在数组的最右边。
小于 pivot 的元素的相对位置和大于 pivot 的元素的相对位置分别为 [9, 5, 3] 和 [12, 14] ,它们在结果数组中的相对顺序需要保留。
示例 2:
输入:nums = [-3,4,3,2], pivot = 2
输出:[-3,2,4,3]
解释:
元素 -3 小于 pivot ,所以在数组的最左边。
元素 4 和 3 大于 pivot ,所以它们在数组的最右边。
小于 pivot 的元素的相对位置和大于 pivot 的元素的相对位置分别为 [-3] 和 [4, 3] ,它们在结果数组中的相对顺序需要保留。
这题,我们可以设置一个2*numsize的数组进行辅助,可以在不改变位置的情况下很好的解决问题
解题代码如下:
/** * 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;
}
边栏推荐
- Flutter开发日志——路由管理
- leveldb简单使用样例
- 206. reverse linked list
- Notes on MySQL core points
- 83. 删除排序链表中的重复元素
- K8s application (IV) - build a redis5 cluster (for direct external access)
- [C language - data storage] how is data stored in memory?
- Comment l'entreprise planifie - t - elle la mise en oeuvre?
- 【新手上路常见问答】关于数据可视化
- 【方案开发】血压计方案压力传感器SIC160
猜你喜欢

机器学习笔记 - 使用TensorFlow的Spatial Transformer网络

【新手上路常见问答】关于数据可视化

MySQL核心点笔记

MSF evasion模块的使用

Which Apple devices support this system update? See if your old apple device supports the latest system

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

kubelet Error getting node 问题求助

Pytorch installation for getting started with deep learning

Create a nodejs based background service using express+mysql
![[C language - function stack frame] analyze the whole process of function call from the perspective of disassembly](/img/c5/40ea5571f187e525b2310812ff2af8.png)
[C language - function stack frame] analyze the whole process of function call from the perspective of disassembly
随机推荐
【方案开发】红外体温计测温仪方案
EN 45545-2t10 precautions for smoke density detection by Horizontal method
SAP OData development tutorial
Sword finger offer II 036 Postfix Expression
Clothing ERP: how do enterprises carry out implementation planning?
876. 链表的中间结点
19. 删除链表的倒数第 N 个结点
844. compare strings with backspace
How to apply for BS 476-7 sample for display? Is it the same as the display
19. delete the penultimate node of the linked list
86. separate linked list
876. intermediate node of linked list
SAP ABAP data types and data objects
Display DIN 4102-1 Class B1 fire test requirements
PVC plastic sheets BS 476-6 determination of flame propagation properties
2095. delete the intermediate node of the linked list
Install jupyter in the specified environment
机器学习笔记 - 卷积神经网络备忘清单
SQL basic query
【方案设计】基于单片机开发的家用血氧仪方案