Given an integer array nums
, move all 0
's to the end of it while maintaining the relative order of the non-zero elements.
Note that you must do this in-place without making a copy of the array.
Example 1:
Input: nums = [0,1,0,3,12] Output: [1,3,12,0,0]
Example 2:
Input: nums = [0] Output: [0]
Constraints:
1 <= nums.length <= 104
-231 <= nums[i] <= 231 - 1
Follow up: Could you minimize the total number of operations done?
program main
implicit none
integer, parameter :: n = 5
integer :: nums(n) = [0, 1, 0, 3, 12]
integer :: i, j
call move_zeros(nums)
write (*, '(A)') 'Expected:'
write (*, '(A)') '[1, 3, 12, 0, 0]'
write (*, '(A)') 'Actual:'
write (*, '(A)') nums
contains
subroutine move_zeros(nums)
implicit none
integer, intent(inout) :: nums(:)
integer :: i, j
i = 1
j = 1
do while (j <= size(nums))
if (nums(j) /= 0) then
nums(i) = nums(j)
i = i + 1
end if
j = j + 1
end do
end subroutine move_zeros
end program main
Expected: [1, 3, 12, 0, 0] Actual:
module move_zeros
contains
subroutine move_zeros_solution(nums)
implicit none
integer, dimension(:), intent(inout) :: nums
integer :: i, j, n
n = size(nums)
i = 1
j = 1
do while (i <= n)
if (nums(i) /= 0) then
nums(j) = nums(i)
j = j + 1
end if
i = i + 1
end do
do while (j <= n)
nums(j) = 0
j = j + 1
end do
end subroutine move_zeros_solution
end module move_zeros
program test_move_zeros
use move_zeros
implicit none
integer, dimension(5) :: nums
nums = [0, 1, 0, 3, 12]
call move_zeros_solution(nums)
write (*,*) nums
nums = [0]
call move_zeros_solution(nums)
write (*,*) nums
end program test_move_zeros
temp.f95:35:4: 35 | nums = [0] | 1 Error: Different shape for array assignment at (1) on dimension 1 (5 and 1)
def moveZeroes(nums):
index = 0
for i in range(len(nums)):
if nums[i] != 0:
nums[index] = nums[i]
index += 1
for i in range(index, len(nums)):
nums[i] = 0
In each language, the algorithm starts by initializing an index
variable with the value of 0. Next, it loops through the input array nums
. If the current element is not zero, it assigns the value of the current element to the position indicated by the index
and then increments the index
. After the first loop, all non-zero elements are placed before the zero elements in the array.
Finally, in the second loop, it assigns the value of 0 to all remaining positions in the array from the current index
to the end of the array.
This algorithm maintains the relative order of non-zero elements as they are moved to the left at the beginning of the array, and ensures that zeros are moved to the end of the array. The algorithm also modifies the given array in-place as required.
void moveZeroes(vector<int>& nums) {
int index = 0;
for (int i = 0; i < nums.size(); i++) {
if (nums[i] != 0) {
nums[index++] = nums[i];
}
}
for (int i = index; i < nums.size(); i++) {
nums[i] = 0;
}
}
In each language, the algorithm starts by initializing an index
variable with the value of 0. Next, it loops through the input array nums
. If the current element is not zero, it assigns the value of the current element to the position indicated by the index
and then increments the index
. After the first loop, all non-zero elements are placed before the zero elements in the array.
Finally, in the second loop, it assigns the value of 0 to all remaining positions in the array from the current index
to the end of the array.
This algorithm maintains the relative order of non-zero elements as they are moved to the left at the beginning of the array, and ensures that zeros are moved to the end of the array. The algorithm also modifies the given array in-place as required.