Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report