当前位置:网站首页>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

边栏推荐
- Report on the double computer experiment of scoring system based on 485 bus
- [pointer] solve the last person left
- Fundamentals of digital circuit (V) arithmetic operation circuit
- [oiclass] maximum formula
- 150 common interview questions for software testing in large factories. Serious thinking is very valuable for your interview
- Database monitoring SQL execution
- Keil5 MDK's formatting code tool and adding shortcuts
- Function: find the root of the equation by Newton iterative method
- Zhejiang University Edition "C language programming experiment and exercise guide (3rd Edition)" topic set
- Thinking about three cups of tea
猜你喜欢

How to learn automated testing in 2022? This article tells you

后台登录系统,JDBC连接数据库,做小案例练习

CSAPP家庭作業答案7 8 9章

基于485总线的评分系统双机实验报告

ucore lab8 文件系统 实验报告

ucore lab7 同步互斥 实验报告

数字电路基础(二)逻辑代数

ucore lab1 系统软件启动过程 实验报告

150 common interview questions for software testing in large factories. Serious thinking is very valuable for your interview

Don't you even look at such a detailed and comprehensive written software test question?
随机推荐
Global and Chinese market of maleic acid modified rosin esters 2022-2028: Research Report on technology, participants, trends, market size and share
数字电路基础(四) 数据分配器、数据选择器和数值比较器
The number of reversing twice in leetcode simple question
Build your own application based on Google's open source tensorflow object detection API video object recognition system (II)
【指针】八进制转换为十进制
Global and Chinese market of portable and handheld TVs 2022-2028: Research Report on technology, participants, trends, market size and share
How to use Moment. JS to check whether the current time is between 2 times
Investment should be calm
ucore lab2 物理内存管理 实验报告
Statistics 8th Edition Jia Junping Chapter XIII Summary of knowledge points of time series analysis and prediction and answers to exercises after class
【指针】删除字符串s中的所有空格
Four methods of exchanging the values of a and B
Want to learn how to get started and learn software testing? I'll give you a good chat today
Fundamentals of digital circuits (II) logic algebra
Common Oracle commands
Fundamentals of digital circuits (III) encoder and decoder
数字电路基础(一)数制与码制
C language do while loop classic Level 2 questions
To brush the video, it's better to see if you have mastered these interview questions. Slowly accumulating a monthly income of more than 10000 is not a dream.
【指针】求解最后留下的人