2149. Rearrange Array Elements by Sign 發表於 2023-02-13 | 分類於 leetcode problemsolutionoption 1 - one pass123456789101112131415161718class Solution {public: vector<int> rearrangeArray(vector<int>& nums) { int l = 0, r = 1, n= nums.size(); vector<int> ret(n,0); for(int i=0;i<n;++i){ if(nums[i]>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)