当前位置:网站首页>Leetcode 2164. 对奇偶下标分别排序(可以,一次过)
Leetcode 2164. 对奇偶下标分别排序(可以,一次过)
2022-06-12 23:30:00 【我不是萧海哇~~~~】
给你一个下标从 0 开始的整数数组 nums 。根据下述规则重排 nums 中的值:
按 非递增 顺序排列 nums 奇数下标 上的所有值。
- 举个例子,如果排序前 nums = [4,1,2,3] ,对奇数下标的值排序后变为 [4,3,2,1] 。奇数下标 1 和 3
的值按照非递增顺序重排。
- 举个例子,如果排序前 nums = [4,1,2,3] ,对奇数下标的值排序后变为 [4,3,2,1] 。奇数下标 1 和 3
按 非递减 顺序排列 nums 偶数下标 上的所有值。
举个例子,如果排序前 nums = [4,1,2,3] ,对偶数下标的值排序后变为 [2,1,4,3] 。偶数下标 0 和 2 的值按照非递减顺序重排。
返回重排 nums 的值之后形成的数组。
示例 1:
输入:nums = [4,1,2,3]
输出:[2,3,4,1]
解释:
首先,按非递增顺序重排奇数下标(1 和 3)的值。
所以,nums 从 [4,1,2,3] 变为 [4,3,2,1] 。
然后,按非递减顺序重排偶数下标(0 和 2)的值。
所以,nums 从 [4,1,2,3] 变为 [2,3,4,1] 。
因此,重排之后形成的数组是 [2,3,4,1] 。
示例 2:
输入:nums = [2,1]
输出:[2,1]
解释:
由于只有一个奇数下标和一个偶数下标,所以不会发生重排。
形成的结果数组是 [2,1] ,和初始数组一样。
提示:
- 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;
}
};
边栏推荐
- Summary of the lowest level error types in PHP
- 测试平台系列(97) 完善执行case部分
- C language: how to give an alias to a global variable?
- Access static variables within class in swift
- InfoQ geek media's 15th anniversary solicitation | brief introduction to the four challenges of building a micro service architecture
- 【LeetCode】300. Longest ascending subsequence
- Alien Skin Exposure X7调色滤镜插件,RAW后期处理工具
- 80 lines of code to realize simple rxjs
- About three-tier architecture and MVC
- [redis sentinel] failed listening on port 26379 (TCP) & sentinel mode no response problem solved
猜你喜欢
AWS lambda: how to store secrets to external APIs- AWS Lambda: How to store secret to external API?
Alien skin exposure X7 color filter plug-in, raw post-processing tool
Shardingsphere-proxy-5.0.0 deployment table implementation (I)
Photoshop:ps how to enlarge a picture without blurring
Teach you how to grab ZigBee packets through cc2531 and parse encrypted ZigBee packets
[Part 8] semaphore source code analysis and application details [key points]
RT thread quick start - experience RT thread
Redis实现短信验证码登录
iShot
Comprehensive analysis of C array
随机推荐
PyTorch常用参数初始化方法:【均匀分布、正态(高斯)分布、Xavier、kaiming、正交矩阵、稀疏矩阵、常数、单位矩阵、零填充】
Analysis report on the "fourteenth five year plan" and the latest development trend of China's medical information industry from 2022 to 2028
Mgr and greatsql resource summary
PostgreSQL 中文社区黑龙江分会和辽宁分会成立啦!
Database system composition
Design of traceid in the project
It is meaningful to define genus, and D can use it to explain semantics
The programmer has worked for 7 years. At the age of 31, he has no choice but to deliver takeout. I really don't want you to go through his journey again
lua 日期时间
The carrying capacity of L2 level ADAS increased by more than 60% year-on-year in January, and domestic suppliers "emerged"
Using baserecyclerviewadapterhelper to implement tree structure
Deep feature synthesis and genetic feature generation, comparison of two automatic feature generation strategies
Alcohol detector based on 51 single chip microcomputer
Analysis report on business model innovation path and operation status of China's app store industry from 2022 to 2028
Sequence maximum return
Alien skin exposure X7 color filter plug-in, raw post-processing tool
【LeetCode】5. Longest Palindromic Substring
[opencv learning] small ticket recognition based on perspective transformation and OCR recognition
Function introduction and common terms of ZABBIX
Go time format assignment