s*i 发帖数: 5025 | 1 The first one?
void placeZerosToEnd (int[] A)
{
if (A == null)
{
return;
}
int endPos = A.length - 1;
for (int i = 0; i < endPos; i++)
{
if (A[i] == 0)
{
A[i] = A[endPos];
A[endPos] = 0;
endPos --;
i--; //need to test this newly swapped element
}
}
} |
|
s*i 发帖数: 5025 | 2 The first one?
void placeZerosToEnd (int[] A)
{
if (A == null)
{
return;
}
int endPos = A.length - 1;
for (int i = 0; i < endPos; i++)
{
if (A[i] == 0)
{
A[i] = A[endPos];
A[endPos] = 0;
endPos --;
i--; //need to test this newly swapped element
}
}
} |
|