Skip to content

Instantly share code, notes, and snippets.

@dbalatero
Created May 1, 2023 04:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbalatero/44ae2a784b50eec1f079673a9bb4bffa to your computer and use it in GitHub Desktop.
Save dbalatero/44ae2a784b50eec1f079673a9bb4bffa to your computer and use it in GitHub Desktop.
def removeZeroes(nums: list[int]) -> list[int]:
count = len(nums)
left = 0
while left < count and nums[left] == 0:
left += 1
right = count - 1
while right > left and nums[right] == 0:
right -= 1
return nums[left:right + 1]
print(removeZeroes([0, 0, 0, 3, 1, 4, 1, 5, 9, 0, 0, 0, 0]))
# > [3, 1, 4, 1, 5, 9]
print(removeZeroes([0, 0, 0]))
# > []
print(removeZeroes([8]))
# > [8]
print(removeZeroes([0, 1, 0, 1, 0]))
# > [1, 0, 1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment