Skip to content

Instantly share code, notes, and snippets.

@asyncins
Created July 5, 2020 03:46
Show Gist options
  • Save asyncins/fc4a9fd7a846148e385bf43f4eedbd8b to your computer and use it in GitHub Desktop.
Save asyncins/fc4a9fd7a846148e385bf43f4eedbd8b to your computer and use it in GitHub Desktop.
[算法-移动零]
# 将零移动到末尾,要求在原数组操作且尽可能减少操作次数
source = [0, 3, 5, 0, 2, 1, 0, 9]
def solution(source):
j = 0
for k, i in enumerate(source):
if i != 0:
source[j], source[k] = i, 0
j += 1
return source
res = solution(source)
print(res) # [3, 5, 2, 1, 9, 0, 0, 0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment