返回介绍

solution / 2100-2199 / 2161.Partition Array According to Given Pivot / README_EN

发布于 2024-06-17 01:03:09 字数 4820 浏览 0 评论 0 收藏 0

2161. Partition Array According to Given Pivot

中文文档

Description

You are given a 0-indexed integer array nums and an integer pivot. Rearrange nums such that the following conditions are satisfied:

  • Every element less than pivot appears before every element greater than pivot.
  • Every element equal to pivot appears in between the elements less than and greater than pivot.
  • The relative order of the elements less than pivot and the elements greater than pivot is maintained.
    • More formally, consider every pi, pj where pi is the new position of the ith element and pj is the new position of the jth element. For elements less than pivot, if i < j and nums[i] < pivot and nums[j] < pivot, then pi < pj. Similarly for elements greater than pivot, if i < j and nums[i] > pivot and nums[j] > pivot, then pi < pj.

Return nums_ after the rearrangement._

 

Example 1:

Input: nums = [9,12,5,10,14,3,10], pivot = 10
Output: [9,5,3,10,10,12,14]
Explanation: 
The elements 9, 5, and 3 are less than the pivot so they are on the left side of the array.
The elements 12 and 14 are greater than the pivot so they are on the right side of the array.
The relative ordering of the elements less than and greater than pivot is also maintained. [9, 5, 3] and [12, 14] are the respective orderings.

Example 2:

Input: nums = [-3,4,3,2], pivot = 2
Output: [-3,2,4,3]
Explanation: 
The element -3 is less than the pivot so it is on the left side of the array.
The elements 4 and 3 are greater than the pivot so they are on the right side of the array.
The relative ordering of the elements less than and greater than pivot is also maintained. [-3] and [4, 3] are the respective orderings.

 

Constraints:

  • 1 <= nums.length <= 105
  • -106 <= nums[i] <= 106
  • pivot equals to an element of nums.

Solutions

Solution 1

class Solution:
  def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
    a, b, c = [], [], []
    for x in nums:
      if x < pivot:
        a.append(x)
      elif x == pivot:
        b.append(x)
      else:
        c.append(x)
    return a + b + c
class Solution {
  public int[] pivotArray(int[] nums, int pivot) {
    int n = nums.length;
    int[] ans = new int[n];
    int k = 0;
    for (int x : nums) {
      if (x < pivot) {
        ans[k++] = x;
      }
    }
    for (int x : nums) {
      if (x == pivot) {
        ans[k++] = x;
      }
    }
    for (int x : nums) {
      if (x > pivot) {
        ans[k++] = x;
      }
    }
    return ans;
  }
}
class Solution {
public:
  vector<int> pivotArray(vector<int>& nums, int pivot) {
    vector<int> ans;
    for (int& x : nums)
      if (x < pivot) ans.push_back(x);
    for (int& x : nums)
      if (x == pivot) ans.push_back(x);
    for (int& x : nums)
      if (x > pivot) ans.push_back(x);
    return ans;
  }
};
func pivotArray(nums []int, pivot int) []int {
  var ans []int
  for _, x := range nums {
    if x < pivot {
      ans = append(ans, x)
    }
  }
  for _, x := range nums {
    if x == pivot {
      ans = append(ans, x)
    }
  }
  for _, x := range nums {
    if x > pivot {
      ans = append(ans, x)
    }
  }
  return ans
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文