/*****************************************************问题描述*************************************************
Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of
the non-zero elements.
Example:
Input: [0,1,0,3,12]
Output: [1,3,12,0,0]
Note:
You must do this in-place without making a copy of the array.
Minimize the total number of operations.
给定一个数值数组,将数组中的0移动到数组最后,其他元素相对顺序不变
/*****************************************************我的解答*************************************************
/**
* @param {number[]} nums
* @return {void} Do not return anything, modify nums in-place instead.
*/
var moveZeroes = function(nums) {
var length = nums.length;
var count = 0;
for(var index = 0; count < length;)
{
if(nums[index] == 0)
{
nums.splice(index, 1);
nums.push(0);
}
else
{
index++
}
count++;
}
};
leetCode刷题记录41_283_Move Zeroes
最新推荐文章于 2021-07-05 14:45:39 发布