Created
July 5, 2020 03:46
-
-
Save asyncins/fc4a9fd7a846148e385bf43f4eedbd8b to your computer and use it in GitHub Desktop.
[算法-移动零]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 将零移动到末尾,要求在原数组操作且尽可能减少操作次数 | |
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