# leetcode 2164. Sort Even and Odd Indices Independently（python）

「这是我参与2022首次更文挑战的第29天，活动详情查看：2022首次更文挑战

### 描述

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.

2. Sort the values at even indices of nums in non-decreasing order.

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].

``````

Note:

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

### 解析

1. 以降序顺序对 nums 奇数索引处的值进行排序
2. 以升序顺序对 nums 的偶数索引处的值进行排序

### 解答

``````class Solution(object):
def sortEvenOdd(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
odd = nums[1::2]
even = nums[::2]
odd.sort(reverse=True)
even.sort()
result = []
while even or odd:
if even:
result.append(even.pop(0))
if odd:
result.append(odd.pop(0))
return result

``````

### 运行结果

``````218 / 218 test cases passed.
Status: Accepted
Runtime: 71 ms
Memory Usage: 13.4 MB
``````

### 原题链接

leetcode.com/contest/wee…