leetcode算法题--调整数组顺序使奇数位于偶数前面

原题链接:https://leetcode-cn.com/problems/diao-zheng-shu-zu-shun-xu-shi-qi-shu-wei-yu-ou-shu-qian-mian-lcof/

vector<int> exchange(vector<int>& nums) {
     
    int n = nums.size();
    int low = 0, high = n - 1;
    while (low < high) {
     
        while (low < high && nums[high] % 2 == 0) high--;
        while (low < high && nums[low] % 2 == 1) low++;
        swap(nums[low],nums[high]);
    }
    return nums;
}

你可能感兴趣的:(算法)