image_2022-02-07_22-45-14.png
41.5 KB
#N2149. Rearrange Array Elements by Sign
problem link
#solution
problem link
#solution
class Solution {
public int[] rearrangeArray(int[] nums) {
int pos[] = new int[nums.length/2], neg[] = new int[nums.length/2], count1=0, count2=0;
for(int n: nums){
if(n>0) pos[count1++]=n;
if(n<0) neg[count2++]=n;
}
for(int i=0; i<nums.length/2; i++){
nums[i*2]=pos[i];
nums[2*i+1]=neg[i];
}
return nums;
}
}