922. Sort Array By Parity II 發表於 2023-02-13 | 分類於 leetcode problemsolutionoption 1 - one pass123456789101112131415161718class Solution {public: vector<int> sortArrayByParityII(vector<int>& nums) { int n = nums.size() , l = 0, r = 1; vector<int> ret(n,0); for(int i = 0;i<n;++i){ if(nums[i]%2==0){ ret[l] = nums[i]; l+=2; } else{ ret[r] = nums[i]; r+=2; } } return ret; }}; analysis time complexity O(n) space complexity O(1)