当前位置:网站首页>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;
}
边栏推荐
- 20. valid brackets
- leveldb简单使用样例
- [share] how do enterprises carry out implementation planning?
- multiplication table
- String类为何final修饰
- 844. 比较含退格的字符串
- 市场上的服装ERP体系到底是哪些类型?
- 面试题 02.02. 返回倒数第 k 个节点
- Complexity analysis of matrix inversion operation (complexity analysis of inverse matrix)
- CUMT learning diary - theoretical analysis of uCOSII - Textbook of Renzhe Edition
猜你喜欢

Talk about how to customize data desensitization

Matlab learning 9- nonlinear sharpening filter for image processing

【C语言-函数栈帧】从反汇编的角度,剖析函数调用全流程

M1 chip guide: M1, M1 pro, M1 Max and M1 ultra

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

智能控制理论小题库

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

Talk about reading the source code

Vagrant mounting pit

openstack详解(二十三)——Neutron其他配置、数据库初始化与服务启动
随机推荐
从企业评价的方历来看ERP软件成功与失利
SAP ABAP data types and data objects
Question d'entrevue 02.02. Renvoie l'avant - dernier noeud K
EN 45545-2t10 precautions for smoke density detection by Horizontal method
智能控制理论小题库
Pytorch installation for getting started with deep learning
83. delete duplicate elements in the sorting linked list
【新手上路常见问答】关于数据可视化
【C语言-数据存储】数据在内存中是怎样存储的?
86. 分隔链表
机器学习笔记 - Kaggle大师Janio Martinez Bachmann的故事
19. 删除链表的倒数第 N 个结点
Typescript high level feature 1 - merge type (&)
206. reverse linked list
SAP OData development tutorial
Notes on MySQL core points
Android 面试笔录(精心整理篇)
What are precompiled, compiled, assembled, linked, static and dynamic libraries
Android interview transcript (carefully sorted out)
基于SIC32F911RET6设计的腕式血压计方案