当前位置:网站首页>Leetcode 2164. Sort odd and even subscripts separately (yes, once)
Leetcode 2164. Sort odd and even subscripts separately (yes, once)
2022-06-12 23:35:00 【I'm not xiaohaiwa~~~~】

I'll give you a subscript from 0 The starting array of integers nums . Rearrange according to the following rules nums The value in :
Press Non-increasing Sequential arrangement nums Odd subscript All values on .
- for instance , If before sorting nums = [4,1,2,3] , After sorting the values of odd subscripts, it becomes [4,3,2,1] . Odd subscript 1 and 3
The values of are rearranged in non increasing order .
- for instance , If before sorting nums = [4,1,2,3] , After sorting the values of odd subscripts, it becomes [4,3,2,1] . Odd subscript 1 and 3
Press The decreasing Sequential arrangement nums Even subscript All values on .
for instance , If before sorting nums = [4,1,2,3] , After sorting the values of even subscripts, it becomes [2,1,4,3] . Even subscript 0 and 2 The values of are rearranged in non decreasing order .
Return rearrangement nums The array formed after the value of .
Example 1:
Input :nums = [4,1,2,3]
Output :[2,3,4,1]
explain :
First , Rearrange in ascending non odd order (1 and 3) Value .
therefore ,nums from [4,1,2,3] Turn into [4,3,2,1] .
then , Rearrange even subscripts in non decreasing order (0 and 2) Value .
therefore ,nums from [4,1,2,3] Turn into [2,3,4,1] .
therefore , The array formed after rearrangement is [2,3,4,1] .
Example 2:
Input :nums = [2,1]
Output :[2,1]
explain :
Because there is only one odd subscript and one even subscript , So there will be no rearrangement .
The resulting array is [2,1] , Just like the initial array .
Tips :
- 1 <= nums.length <= 100
- 1 <= nums[i] <= 100
Code:
class Solution {
public:
vector<int> sortEvenOdd(vector<int>& nums) {
vector<int>even;
vector<int>odd;
for(int i=0;i<nums.size();i++)
{
if(i%2)
{
odd.push_back(nums[i]);
}
else
even.push_back(nums[i]);
}
sort(odd.begin(),odd.end(),greater<int>());
sort(even.begin(),even.end());
vector<int>res;
for(int i=0;i<even.size();i++)
{
res.push_back(even[i]);
if(i<(odd.size()))
res.push_back(odd[i]);
}
return res;
}
};
边栏推荐
- Message queue directory
- Report on the "fourteenth five year plan" and strategic strategy recommendations for China's intellectual property protection industry 2022 ~ 2028
- 启牛帮开通的股票账户是安全可信的吗?
- Lua loop statement
- Access static variables within class in swift
- Novice must see! How rust beginners write gear smart contracts (1)
- Dix points de défense clés dans les exercices d'attaque et de défense détaillés
- Leetcode 2164. 对奇偶下标分别排序(可以,一次过)
- Use js to listen for Keydown event
- LeetCode 890 查找和替换模式[map] HERODING的LeetCode之路
猜你喜欢

For product managers, which of the two certificates, PMP and NPDP, is more authoritative?

Gb28181 protocol -- alarm

Colab tutorial (super detailed version) and colab pro/colab pro+ usage evaluation

Unprecedented analysis of Milvus source code architecture
C语言:如何给全局变量起一个别名?

【建议收藏】通俗易懂图解网络知识-第一篇

Chapter 8 - shared model JUC

Test platform series (97) perfect the case part

The Milvus graphical management tool Attu is coming!

Colab教程(超级详细版)及Colab Pro/Colab Pro+使用评测
随机推荐
Deep learning neural network: implementation method of convolution [direct method (no loss of precision), GEMM (matrix multiplication, no loss of precision), FFT (Fourier transform, loss of precision)
2022起重机械指挥上岗证题目模拟考试平台操作
Analysis report on the 14th five year development plan and operation mode of China's hazardous waste treatment industry from 2022 to 2028
Dry goods sharing | BitSet application details
Global and Chinese Melamine Industry Development Research and prospect trend report 2022-2028
Lua loop statement
Restrictions on MySQL function creation
Face detection: mtcnn
〖Kubernetes指南④〗Pod快速入门
LeetCode 146. LRU cache
[Yugong series] wechat applet in February 2022 - Reference
Opencv source code compilation
China's new generation information technology industry "14th five year plan" special planning and innovation strategic direction report 2022 ~ 2028
Leetcode 890 finding and replacing patterns [map] the leetcode path of heroding
Analysis report on production and marketing demand and investment forecast of China's Melamine Industry from 2022 to 2028
模型过拟合-解决方案(二):Dropout
QT actual combat case (38) -- using qpprocess class to realize the function of starting process
[kubernetes guide ④] pod quick start
Find out the data that can match the keyword key in field 1 or field 2 in the database table. If you want to display the matching data in field 1 first
MySQL基础篇视图的总结