当前位置: 首页 > news >正文

LeetCode 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] ,它们在结果数组中的相对顺序需要保留。

提示:

1 <= nums.length <= 105
-106 <= nums[i] <= 106
pivot 等于 nums 中的一个元素。

法一:按顺序保存下来小于pivot和大于pivot的数,再拼接:

class Solution {
public:vector<int> pivotArray(vector<int>& nums, int pivot) {vector<int> small;vector<int> big;int pivotNum = 0;for (int num : nums){if (num < pivot){small.push_back(num);}else if (num > pivot){big.push_back(num);}else{++pivotNum;}}for (int i = 0; i < pivotNum; ++i){small.push_back(pivot);}small.insert(small.end(), big.begin(), big.end());return small;}
};

如果nums的长度为n,此算法时间复杂度为O(n),空间复杂度为O(n)。

法二:直接在结果数组中构建答案,先正向遍历nums,把小于pivot的数按顺序放在左边,然后反向遍历nums,把大于pivot的数按顺序放在右边,中间填充pivot即可:

class Solution {
public:vector<int> pivotArray(vector<int>& nums, int pivot) {vector<int> ans(nums.size());int smallIndex = 0;for (int num : nums){if (num < pivot){ans[smallIndex++] = num;}}int bigIndex = nums.size() - 1;for (vector<int>::reverse_iterator it = nums.rbegin(); it != nums.rend(); ++it){if (*it > pivot){ans[bigIndex--] = *it;}}while (smallIndex <= bigIndex){ans[smallIndex++] = pivot;ans[bigIndex--] = pivot;}return ans;}
};

如果nums的长度为n,此算法时间复杂度为O(n),空间复杂度为O(1)。本解法也可以一遍正向遍历,把大于pivot的值在ans的最后从右往左排,最后再reverse一下大于pivot的值即可:

class Solution {
public:vector<int> pivotArray(vector<int>& nums, int pivot) {vector<int> ans(nums.size(), pivot);int smallIndex = 0;int bigIndex = nums.size() - 1;for (int num : nums){if (num < pivot){ans[smallIndex++] = num;}else if (num > pivot){ans[bigIndex--] = num;}}reverse(ans.begin() + bigIndex + 1, ans.end());return ans;}
};
http://www.lryc.cn/news/310623.html

相关文章:

  • ip获取+归属地实现
  • Python的错误和异常
  • C语言-------指针进阶(2)
  • Spring El表达式官方文档学习
  • RK3568 android11 调试陀螺仪模块 MPU6500
  • 【HTML】HTML基础6.1(表格以及常见属性)
  • 数字电路三宝:锁存器、寄存器和触发器
  • VLC相关资源及使用方法
  • 4_相机透镜畸变
  • 微信小程序(四十六)登入界面-进阶版
  • CSP-201712-2-游戏
  • 记录SSM项目集成Spring Security 4.X版本 之 加密验证和记住我功能
  • [AutoSar]BSW_Com09 CAN driver 模块FULL(BASIC)CAN、FIFO选择
  • WPF真入门教程30--顺风物流单据管理系统
  • Elasticsearch:向量相似度计算 - 可笑的速度
  • 两数相加的问题
  • 微信小程序的单位
  • 软考通过率真的低吗?
  • 国际视频编解码标准提案下载地址
  • 程序员是如何看待“祖传代码”的?
  • Python爬虫之爬取并下载哔哩哔哩视频
  • python 脚本设置输出颜色
  • 安卓websocket(客服端和服务端写在app端) 案例
  • C++面试宝典第34题:整数反序
  • 微信商城小程序设计
  • 如何合理布局子图--确定MATLAB的subplot子图位置参数
  • 【MySQL】基于Docker搭建MySQL一主二从集群
  • k8s 集群调度,标签,亲和性和反亲和性,污点和容忍,pod启动状态 排错详解
  • Idea 启动报错 failed to create jvm:jvm path url
  • 20款Visual Studio实用插件推荐