当前位置:网站首页>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;
}
边栏推荐
- 机器学习笔记 - Kaggle大师Janio Martinez Bachmann的故事
- Typescript -- preliminary study of variable declaration
- Which Apple devices support this system update? See if your old apple device supports the latest system
- kubelet Error getting node 问题求助
- Kubelet error getting node help
- 【ERP体系】专业技术层面的评估,你了解多少?
- CUMT学习日记——ucosII理论解析—任哲版教材
- 【237. 删除链表中的节点】
- Display DIN 4102-1 Class B1 fire test requirements
- SAP OData development tutorial
猜你喜欢

TextView文本大小自动适配与TextView边距的去除
![[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

Install jupyter in the specified environment

shell脚本之sed详解 (sed命令 , sed -e , sed s/ new / old / ... )

Notes on MySQL core points

Some learning records I=

Create a nodejs based background service using express+mysql

openstack详解(二十一)——Neutron组件安装与配置

MySQL核心点笔记

ArcGIS 10.9.1 地质、气象体元数据处理及服务发布调用
随机推荐
机器学习笔记 - 卷积神经网络备忘清单
SAP abap 数据类型与数据对象
MSF evasion模块的使用
206. 反转链表
openstack详解(二十二)——Neutron插件配置
Flutter开发日志——路由管理
SAP ABAP internal table classification, addition, deletion, modification and query
小型制氧机解决方案PCBA电路板开发
Iso8191 test is mentioned in as 3744.1. Are the two tests the same?
206. reverse linked list
【方案设计】基于单片机开发的家用血氧仪方案
【新手上路常见问答】关于数据可视化
Erreur de démarrage MySQL "BIND on TCP / IP Port: Address already in use"
445. adding two numbers II
Matlab learning 8- linear and nonlinear sharpening filtering and nonlinear smoothing filtering of image processing
[share] how do enterprises carry out implementation planning?
[C language - function stack frame] analyze the whole process of function call from the perspective of disassembly
山东大学项目实训(四)—— 微信小程序扫描web端二维码实现web端登录
Kubelet error getting node help
MySQL核心点笔记