其他分享
首页 > 其他分享> > 剑指OFFER----面试题21. 调整数组顺序使奇数位于偶数前面

剑指OFFER----面试题21. 调整数组顺序使奇数位于偶数前面

作者:互联网

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

思路:

  快排partition

 

代码:

class Solution {
public:
    vector<int> exchange(vector<int>& nums) {
        int l = 0, r = nums.size() - 1;
        while (l <= r) {
            while (l <= r && nums[l] % 2 == 1) l++;
            while (l <= r && nums[r] % 2 == 0) r--;
            if (l < r) swap(nums[l], nums[r]);
        }
        return nums;
    }
};

 

标签:面试题,mian,cn,shu,nums,OFFER,vector,21
来源: https://www.cnblogs.com/clown9804/p/12341959.html