当前位置:网站首页>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;
}
};
边栏推荐
- The development trend of digital collections!
- CS for mobile security [nethunter]
- C语言:如何给全局变量起一个别名?
- Actual combat | inductance element positioning -- detailed explanation of Halcon and opencv implementation (with source code)
- PostgreSQL 中文社区黑龙江分会和辽宁分会成立啦!
- RT thread quick start - experience RT thread
- Avoid using asp Net core 3.0 to inject services for startup classes
- 2202 resume making
- SAP QM qp03 displays an inspection plan with multiple specs inspection features
- [opencv learning] use the Tesseract OCR movement to recognize numbers
猜你喜欢

PostgreSQL 中文社区黑龙江分会和辽宁分会成立啦!

Access static variables within class in swift

2202-简历制作

OpenCV源代码编译

Colab教程(超级详细版)及Colab Pro/Colab Pro+使用评测

Actual combat | inductance element positioning -- detailed explanation of Halcon and opencv implementation (with source code)

测试平台系列(97) 完善执行case部分

Preparing for the Blue Bridge Cup Day11__ Basic operation of serial port communication

基于Three.js海上风电数字孪生三维效果

Redis realizes SMS verification code login
随机推荐
Module 8 operation
Hongmeng starts
Redis实现短信验证码登录
Leetcode 2200. 找出数组中的所有 K 近邻下标(可以,一次过)
Model over fitting - solution (II): dropout
Research and Analysis on the development of China's Melamine Industry from 2022 to 2028 and market prospect forecast report
CST learning: four element array design of circular patch antenna (III) array formation and parallel excitation
Opencv source code compilation
The "fourteenth five year plan" development plan and panoramic strategic analysis report of China's information and innovation industry 2022 ~ 2028
Gradient accumulation in pytorch [during the experiment, due to the limitation of GPU video memory, the batch\u size can no longer be increased. To solve this problem, the gradient accumulation method
Analysis report on the 14th five year development plan and operation mode of China's hazardous waste treatment industry from 2022 to 2028
OpenCV源代码编译
Preparing for the Blue Bridge Cup Day11__ Basic operation of serial port communication
线上真实排队系统重构案例分享——实战篇
利率降低导致债券价格上涨
Matters of parent-child class construction method in inheritance
LeetCode —— 26. Remove duplicates from an ordered array
Avoid using asp Net core 3.0 to inject services for startup classes
Pytorch common parameter initialization methods: [uniform distribution, normal (Gaussian) distribution, Xavier, Kaiming, orthogonal matrix, sparse matrix, constant, identity matrix, zero filling]
For product managers, which of the two certificates, PMP and NPDP, is more authoritative?