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

边栏推荐
- Pointer -- eliminate all numbers in the string
- 后台登录系统,JDBC连接数据库,做小案例练习
- Oracle foundation and system table
- Description of Vos storage space, bandwidth occupation and PPS requirements
- 王爽汇编语言详细学习笔记二:寄存器
- High concurrency programming series: 6 steps of JVM performance tuning and detailed explanation of key tuning parameters
- Zhejiang University Edition "C language programming experiment and exercise guide (3rd Edition)" topic set
- My first blog
- MySQL development - advanced query - take a good look at how it suits you
- 数字电路基础(一)数制与码制
猜你喜欢

Réponses aux devoirs du csapp 7 8 9

"If life is just like the first sight" -- risc-v

What is an index in MySQL? What kinds of indexes are commonly used? Under what circumstances will the index fail?

DVWA exercise 05 file upload file upload

Four methods of exchanging the values of a and B

Leetcode simple question: check whether two strings are almost equal

Logstack introduction and deployment -- elasticstack (elk) work notes 019

Fundamentals of digital circuit (V) arithmetic operation circuit

5分钟掌握机器学习鸢尾花逻辑回归分类

Keil5 MDK's formatting code tool and adding shortcuts
随机推荐
使用 flask_whooshalchemyplus jieba实现flask的全局搜索
Fundamentals of digital circuits (I) number system and code system
[Ogg III] daily operation and maintenance: clean up archive logs, register Ogg process services, and regularly back up databases
Numpy快速上手指南
Global and Chinese markets of electronic grade hexafluorobutadiene (C4F6) 2022-2028: Research Report on technology, participants, trends, market size and share
[pointer] octal to decimal
What are the business processes and differences of the three basic business modes of Vos: direct dial, callback and semi direct dial?
王爽汇编语言详细学习笔记二:寄存器
Zhejiang University Edition "C language programming experiment and exercise guide (3rd Edition)" topic set
Sleep quality today 81 points
Function: calculates the number of uppercase letters in a string
【指针】求二维数组中最大元素的值
Statistics 8th Edition Jia Junping Chapter 4 Summary and after class exercise answers
Numpy Quick Start Guide
Rearrange spaces between words in leetcode simple questions
[pointer] counts the number of times one string appears in another string
Réponses aux devoirs du csapp 7 8 9
DVWA exercise 05 file upload file upload
Global and Chinese market of goat milk powder 2022-2028: Research Report on technology, participants, trends, market size and share
指针:最大值、最小值和平均值