返回介绍

solution / 2100-2199 / 2164.Sort Even and Odd Indices Independently / README_EN

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

2164. Sort Even and Odd Indices Independently

中文文档

Description

You are given a 0-indexed integer array nums. Rearrange the values of nums according to the following rules:

  1. Sort the values at odd indices of nums in non-increasing order.
    • For example, if nums = [4,1,2,3] before this step, it becomes [4,3,2,1] after. The values at odd indices 1 and 3 are sorted in non-increasing order.
  2. Sort the values at even indices of nums in non-decreasing order.
    • For example, if nums = [4,1,2,3] before this step, it becomes [2,1,4,3] after. The values at even indices 0 and 2 are sorted in non-decreasing order.

Return _the array formed after rearranging the values of_ nums.

 

Example 1:

Input: nums = [4,1,2,3]
Output: [2,3,4,1]
Explanation: 
First, we sort the values present at odd indices (1 and 3) in non-increasing order.
So, nums changes from [4,1,2,3] to [4,3,2,1].
Next, we sort the values present at even indices (0 and 2) in non-decreasing order.
So, nums changes from [4,1,2,3] to [2,3,4,1].
Thus, the array formed after rearranging the values is [2,3,4,1].

Example 2:

Input: nums = [2,1]
Output: [2,1]
Explanation: 
Since there is exactly one odd index and one even index, no rearrangement of values takes place.
The resultant array formed is [2,1], which is the same as the initial array. 

 

Constraints:

  • 1 <= nums.length <= 100
  • 1 <= nums[i] <= 100

Solutions

Solution 1

class Solution:
  def sortEvenOdd(self, nums: List[int]) -> List[int]:
    a = sorted(nums[::2])
    b = sorted(nums[1::2], reverse=True)
    nums[::2] = a
    nums[1::2] = b
    return nums
class Solution {
  public int[] sortEvenOdd(int[] nums) {
    int n = nums.length;
    int[] a = new int[(n + 1) >> 1];
    int[] b = new int[n >> 1];
    for (int i = 0, j = 0; j < n >> 1; i += 2, ++j) {
      a[j] = nums[i];
      b[j] = nums[i + 1];
    }
    if (n % 2 == 1) {
      a[a.length - 1] = nums[n - 1];
    }
    Arrays.sort(a);
    Arrays.sort(b);
    int[] ans = new int[n];
    for (int i = 0, j = 0; j < a.length; i += 2, ++j) {
      ans[i] = a[j];
    }
    for (int i = 1, j = b.length - 1; j >= 0; i += 2, --j) {
      ans[i] = b[j];
    }
    return ans;
  }
}
class Solution {
public:
  vector<int> sortEvenOdd(vector<int>& nums) {
    int n = nums.size();
    vector<int> a;
    vector<int> b;
    for (int i = 0; i < n; ++i) {
      if (i % 2 == 0)
        a.push_back(nums[i]);
      else
        b.push_back(nums[i]);
    }
    sort(a.begin(), a.end());
    sort(b.begin(), b.end(), greater<int>());
    vector<int> ans(n);
    for (int i = 0, j = 0; j < a.size(); i += 2, ++j) ans[i] = a[j];
    for (int i = 1, j = 0; j < b.size(); i += 2, ++j) ans[i] = b[j];
    return ans;
  }
};
func sortEvenOdd(nums []int) []int {
  n := len(nums)
  var a []int
  var b []int
  for i, v := range nums {
    if i%2 == 0 {
      a = append(a, v)
    } else {
      b = append(b, v)
    }
  }
  ans := make([]int, n)
  sort.Ints(a)
  sort.Slice(b, func(i, j int) bool {
    return b[i] > b[j]
  })
  for i, j := 0, 0; j < len(a); i, j = i+2, j+1 {
    ans[i] = a[j]
  }
  for i, j := 1, 0; j < len(b); i, j = i+2, j+1 {
    ans[i] = b[j]
  }
  return ans
}

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

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

发布评论

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