2149. 按符号重排数组
解题思路
另建两个容器,分别存储正负整数,然后依次正负相间放入nums
class Solution {
public:
vector<int> rearrangeArray(vector<int>& nums) {
int n=nums.size(),j=1;
vector<int>a,b;
for(auto i:nums){
if(i<0)b.push_back(i);
else a.push_back(i);
}
for(int i=0;i<nums.size();i++){
if(i%2==0)nums[i]=a[i/2];
else nums[i]=b[i/2];
}
return nums;
}
};
标签:nums,int,另建,力扣,vector,正负,numsclass,2149
From: https://blog.51cto.com/u_15974747/8285568