当前位置:网站首页>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
边栏推荐
- MySQL development - advanced query - take a good look at how it suits you
- 数字电路基础(五)算术运算电路
- "If life is just like the first sight" -- risc-v
- [pointer] counts the number of times one string appears in another string
- Statistics 8th Edition Jia Junping Chapter 2 after class exercises and answer summary
- What is an index in MySQL? What kinds of indexes are commonly used? Under what circumstances will the index fail?
- Four methods of exchanging the values of a and B
- Function: find the maximum common divisor and the minimum common multiple of two positive numbers
- With 27K successful entry ByteDance, this "software testing interview notes" has benefited me for life
- My first blog
猜你喜欢
CSAPP家庭作業答案7 8 9章
Build your own application based on Google's open source tensorflow object detection API video object recognition system (II)
Logstack introduction and deployment -- elasticstack (elk) work notes 019
Don't you even look at such a detailed and comprehensive written software test question?
What level do 18K test engineers want? Take a look at the interview experience of a 26 year old test engineer
Wang Shuang's detailed learning notes of assembly language II: registers
Cadence physical library lef file syntax learning [continuous update]
Maximum nesting depth of parentheses in leetcode simple questions
Es full text index
数字电路基础(三)编码器和译码器
随机推荐
Global and Chinese market of barrier thin film flexible electronics 2022-2028: Research Report on technology, participants, trends, market size and share
后台登录系统,JDBC连接数据库,做小案例练习
Build your own application based on Google's open source tensorflow object detection API video object recognition system (II)
Public key box
Login the system in the background, connect the database with JDBC, and do small case exercises
The salary of testers is polarized. How to become an automated test with a monthly salary of 20K?
ucore lab7 同步互斥 实验报告
Differences between select, poll and epoll in i/o multiplexing
What is an index in MySQL? What kinds of indexes are commonly used? Under what circumstances will the index fail?
【指针】查找最大的字符串
Statistics 8th Edition Jia Junping Chapter XIII Summary of knowledge points of time series analysis and prediction and answers to exercises after class
王爽汇编语言学习详细笔记一:基础知识
My first blog
Fundamentals of digital circuit (V) arithmetic operation circuit
Thinking about three cups of tea
Get started with Matplotlib drawing
About the garbled code problem of superstar script
1. Payment system
[pointer] octal to decimal
What level do 18K test engineers want? Take a look at the interview experience of a 26 year old test engineer