Sword refers to Offer 21. Adjust the array order so that the odd numbers are in front of the even numbers (C++) double pointer

Enter an integer array 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, and all even numbers are in the second half of the array.

Example:

输入:nums = [1,2,3,4]
输出:[1,3,2,4] 
注:[3,1,2,4] 也是正确的答案之一。

prompt:

0 <= nums.length <= 50000
1 <= nums[i] <= 10000

Double pointer

1. Define the head pointer left and the tail pointer right.
2. Left moves to the right until the value it points to is even
3. Right moves to the left until the value it points to is odd
4. Exchange nums[left] and nums [right].
5. Repeat the above operation until left == right.

Problem-solving ideas:

Consider defining the left and right ends of the double pointer i and j in a columnar array and execute it in a loop:

1. Pointer i looks for an even number from left to right;
2. Pointer j looks for an odd number from right to left;
3. Exchange even number nums[i] and odd number nums[j].
4. It can always be guaranteed: pointer i is all odd numbers on the left, and pointer j is even numbers on the right.
Insert picture description here

Algorithm flow:

1. Initialization : i, j double pointers, respectively point to the left and right ends of the array nums;
2. Loop exchange : jump out when i = j;
2-1. If the pointer i encounters an odd number, execute i = i + 1 to skip until Find an even number;
2-2. When the pointer j encounters an even number, execute j = j-1 to skip until an odd number is found;
2-3. Exchange nums[i] and nums[j] values;
3. Return value : Return modified The nums array.

Author: jyd
link: https: //leetcode-cn.com/problems/diao-zheng-shu-zu-shun-xu-shi-qi-shu-wei-yu-ou-shu-qian-mian-lcof/solution /mian-shi-ti-21-diao-zheng-shu-zu-shun-xu-shi-qi-4/
Source: LeetCode (LeetCode)
copyright belongs to the author. For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.

class Solution {
    
    
public:
    vector<int> exchange(vector<int>& nums) {
    
    
        int left = 0, right = nums.size() - 1;
        while (left < right) {
    
    
            if ((nums[left] & 1) != 0) {
    
    //(nums[left] & 1) != 0判断是否为奇数
                left ++;
                continue;
            }
            if ((nums[right] & 1) != 1) {
    
    //判断是否为偶数
                right --;
                continue;
            }
            swap(nums[left++], nums[right--]);//两者交换
        }
        return nums;
    }
};

Complexity analysis:

Time complexity O(N): N is the length of the array nums, and the double pointers i and j traverse the entire array together.
Space complexity O(1): Double pointers i, j use constant extra space.

Author: huwt
link: https: //leetcode-cn.com/problems/diao-zheng-shu-zu-shun-xu-shi-qi-shu-wei-yu-ou-shu-qian-mian-lcof/solution /ti-jie-shou-wei-shuang-zhi-zhen-kuai-man-shuang-zh/
Source: LeetCode (LeetCode)
copyright belongs to the author. For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.

Guess you like

Origin blog.csdn.net/qq_30457077/article/details/114987853
Recommended