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;
    }
};
  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值