题目:
给一个数组 nums 写一个函数将 0 移动到数组的最后面,非零元素保持原数组的顺序
给出 nums = [0,
1, 0, 3, 12]
, 调用函数之后, nums = [1,
3, 12, 0, 0]
.
Function:
public class MoveZero
{
public LinkedList<int> MoveZeroes(int[] nums)
{
// Write your code here
var numList = new LinkedList<int>(nums);
int length = 0;
while (numList.Contains(0))
{
numList.Remove(0);
length++;
}
for (int i = 0; i < length; i++)
{
numList.AddLast(0);
}
return numList;
}
}
*利用List处理数组,返回List.*匹配0,移除。记录长度。
*循环长度,List末尾加0.返回即可。