Method one: square add sorting
For each element of an array of square first, and then sorted,
Method 2: double needle method: because this is an ordered array, two Pointers point to the right and the left, then square value comparison of the two, and then put the larger in the right, in turn,
The class Solution {
Public:
Vector
Int len=A.s considering ();
Vector
Int a=0, b=len - 1, I=len - 1;
While (I & gt;=0)
{
Int lef=A * A [A]; [A] * int rig=A [b] A [b];
If (lef & gt; Rig)
{
Ans [I]=lef.
a++;
}
The else ans [I]=rig, b -;
i--;
}
Return ans.
}
};
Method 2: binary finding boundary, and then extend
The class Solution {
Public:
Vector
Int len=A.s considering ();
Vector
Int l=1, r=len - 1, mid=0;
While (l & lt; R)
{
Mid=(int) (l + r + 1) & gt;> 1;
Int len=A.s considering ();
If (A/mid & lt; L==0) mid;
The else r=mid - 1;
}
R=l + 1;
for (int i=0; i
Ans. The push_back (A * A [r] [r]);
R++;
}
Else if (r & gt; Len)={
Ans. The push_back (A [l] * [l] A);
L -;
}
The else {
If ((A [l] * [l] A) & lt;=(A * A [r] [r]]) {
Int temp=A * A [l] [l];
Ans. The push_back (temp);
L -;
}
The else {
Ans. The push_back ((A * A [r] [r]));
R++;
}
}
}
for (int i=0; i
Cout & lt;
Return ans.
}
};
At first, the problems encountered during definition vector
In fact is equal to make the following error:
Const int size=7.
Int ia [size]={0, 1, 1, 2, 3, 5, 8};
Vector
Ivec. Push_back (ia [ix]);
Ivec contain 14 elements at the end of the program, ia elements from the eighth element inserted,