From 459880ae00ab997e6dde3058e56bdf6923649d13 Mon Sep 17 00:00:00 2001 From: JimmyChavada Date: Sun, 24 Dec 2023 13:52:38 -0500 Subject: [PATCH] Create python code for move all zeros to end of array --- Move_all_zeroes_to_end_of_array/code.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Move_all_zeroes_to_end_of_array/code.py diff --git a/Move_all_zeroes_to_end_of_array/code.py b/Move_all_zeroes_to_end_of_array/code.py new file mode 100644 index 0000000..7e519a4 --- /dev/null +++ b/Move_all_zeroes_to_end_of_array/code.py @@ -0,0 +1,23 @@ +class Solution(object): + def moveZeroes(self, nums): + """ + :type nums: List[int] + :rtype: None Do not return anything, modify nums in-place instead. + """ + + non_zero_elements = [] + count_of_zero = 0 + + # Traverse the array and add non-zero elements to the list + for number in nums: + if number != 0: + non_zero_elements.append(number) + else: + count_of_zero += 1 + + # Add zeroes to the list + non_zero_elements.extend([0] * count_of_zero) + + # Modify nums in-place + for i in range(len(nums)): + nums[i] = non_zero_elements[i]