
class Solution {
public:
vector<int> exchange(vector<int>& nums) {
int low = 0, fast = 0;
while (fast < nums.size()) {
if (nums[fast] & 1) {
swap(nums[low], nums[fast]);
low ++;
}
fast ++;
}
return nums;
}
};
class Solution {
public:
vector<int> exchange(vector<int>& nums) {
sort(nums.begin(),nums.end(),[](int a,int b){return a%2>b%2;});
return nums;
}
};
这篇博客介绍了两种C++实现方式,交换数组中奇数和偶数位置的元素。第一种使用双指针,第二种利用排序。这两种方法展示了不同的算法思路在数组操作上的应用。
372

被折叠的 条评论
为什么被折叠?



