1089. 复写零

 

 

思路:

果然做题最难得还是读懂题意:遍历arr,遇到一个0则复制一个,最后arr长度不变;
所以:
遍历arr,在下标为i处遇到0则insert(i,0),并将最后一个元素pop掉,以保证arr长度不变;
 1 class Solution(object):
 2     def duplicateZeros(self, arr):
 3         """
 4         :type arr: List[int]
 5         :rtype: None Do not return anything, modify arr in-place instead.
 6         """
 7         i = 0
 8         while i < len(arr) - 1:
 9             if arr[i] == 0:
10                 arr.insert(i, 0)
11                 arr.pop()
12                 i += 2
13             else:
14                 i += 1
 
原文地址:https://www.cnblogs.com/panweiwei/p/12800437.html