Get the App
SLTechnology News&Howtos  ›  Development  › 

How LeetCode adjusts the array order so that odd numbers precede even numbers

Shulou Source: shulou.com Published: 2022-06-02 06:35:30 10月03日 Update

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

Tags: Odd array even order adjustment code content idea article between value position interest function first half half parity parity boy Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi MariaDB OPPO Reno Microsoft Shulou Technology