Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How does JZ13 adjust the order of arrays so that odd numbers precede even ones

Shulou Source: shulou.com Published: 2022-06-02 05:23:04 10月03日 Update

This article introduces the knowledge of "how to adjust the array order so that the odd number is in front of the even number". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

JZ13 adjusts the array order so that the odd number comes before the even number

Enter an array of integers and implement a function to adjust the order of the numbers in the array so that all odd numbers are in the first half of the array, all even numbers are in the second half of the array, and the relative positions between odd and odd numbers remain the same.

Train of thought

Stable sorting is required

# @ param array int Integer one-dimensional Array # @ return int Integer one-dimensional Array # class Solution:''twice traversing def reOrderArray (self Array): newarray = [] for i in array: if I% 2 cycles 0: newarray.append (I) for i in array: if I% 2 cycles 0: newarray.append (I) return newarray 'once traversing def reOrderArray (self, array): I, j = 0 0 while 1: if I > = len (array): break if array [I]% 2 array.insert (j, array.pop (I)) j + = 1i + = 1 return array'if _ name__ ='_ _ main__': sl = Solution () print (sl.reOrderArray 7]) print (sl.reOrderArray) print (sl.reOrderArray ([])) print ([[])) "how to adjust the array order so that odd numbers precede even numbers" is introduced here. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Array even odd order adjustment content more knowledge one-dimensional practical learned next between position function front half half dilemma practice train of thought Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Xiaomi Docker MySQL Linux