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 LeetCode adjusts the array order so that odd numbers precede even numbers

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

Share

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

This article mainly introduces how LeetCode adjusts the array order so that odd numbers are located in front of even numbers. The article is very detailed and has certain reference value. Interested friends must read it!

Input an array of integers, implement a function to adjust the order of the numbers in the array, so that all odd numbers are located in the first half of the array, all even numbers are located in the second half of the array, and ensure that the relative position between odd and odd, even and even numbers remains unchanged.

Idea 1: Create two arrays to store odd and even numbers respectively, odd numbers enter v1, even numbers enter v2, and finally merge

Code:

void reOrderArray(vector &array){ vector v1; vector v2; for(int i=0;i

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

Development

Wechat

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

12
Report