当前位置:网站首页>Sorting odd and even subscripts respectively for leetcode simple problem
Sorting odd and even subscripts respectively for leetcode simple problem
2022-07-06 15:07:00 【·Starry Sea】
subject
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 .
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
source : Power button (LeetCode)
Their thinking
Thanks to the python Convenient slicing , We can sort elements with even subscripts and elements with odd subscripts separately .
class Solution:
def sortEvenOdd(self, nums: List[int]) -> List[int]:
nums[::2]=sorted(nums[::2])
nums[1::2]=sorted(nums[1::2],reverse=True)
return nums
边栏推荐
- Keil5-MDK的格式化代码工具及添加快捷方式
- Function: find 1-1/2+1/3-1/4+1/5-1/6+1/7-... +1/n
- Maximum nesting depth of parentheses in leetcode simple questions
- 【指针】求字符串的长度
- C language learning summary (I) (under update)
- Global and Chinese market of barrier thin film flexible electronics 2022-2028: Research Report on technology, participants, trends, market size and share
- Global and Chinese markets of Iam security services 2022-2028: Research Report on technology, participants, trends, market size and share
- 【指针】统计一字符串在另一个字符串中出现的次数
- 指针--剔除字符串中的所有数字
- Face and eye recognition based on OpenCV's own model
猜你喜欢
Query method of database multi table link
Maximum nesting depth of parentheses in leetcode simple questions
数字电路基础(五)算术运算电路
Fundamentals of digital circuits (II) logic algebra
Database monitoring SQL execution
Interview Essentials: what is the mysterious framework asking?
“人生若只如初见”——RISC-V
Report on the double computer experiment of scoring system based on 485 bus
ucore lab2 物理内存管理 实验报告
What level do 18K test engineers want? Take a look at the interview experience of a 26 year old test engineer
随机推荐
Numpy Quick Start Guide
How to learn automated testing in 2022? This article tells you
Global and Chinese markets of electronic grade hexafluorobutadiene (C4F6) 2022-2028: Research Report on technology, participants, trends, market size and share
Description of Vos storage space, bandwidth occupation and PPS requirements
后台登录系统,JDBC连接数据库,做小案例练习
Global and Chinese market of goat milk powder 2022-2028: Research Report on technology, participants, trends, market size and share
Soft exam information system project manager_ Project set project portfolio management --- Senior Information System Project Manager of soft exam 025
Matplotlib绘图快速入门
Build your own application based on Google's open source tensorflow object detection API video object recognition system (I)
【指针】求解最后留下的人
1.支付系统
High concurrency programming series: 6 steps of JVM performance tuning and detailed explanation of key tuning parameters
[issue 18] share a Netease go experience
Rearrange spaces between words in leetcode simple questions
数字电路基础(五)算术运算电路
Summary of thread implementation
Global and Chinese market of portable and handheld TVs 2022-2028: Research Report on technology, participants, trends, market size and share
CSAPP家庭作业答案7 8 9章
Thinking about three cups of tea
Capitalize the title of leetcode simple question