题目
代码部分(Python 72ms 91.35%)
class Solution:
def duplicateZeros(self, arr: List[int]) -> None:
"""
Do not return anything, modify arr in-place instead.
"""
n = len(arr)
i = 0
while(i < n):
if(arr[i] == 0):
arr.insert(i+1, 0)
arr.pop()
i += 2
else:
i += 1
代码部分(Java 2ms 92.96%)
class Solution {
public void duplicateZeros(int[] arr) {
int len = arr.length;
int[] nums = new int[len];
System.arraycopy(arr, 0, nums, 0, len);
int n = 0;
for(int i = 0; i < len; i++){
if(n >= len) break;
if(nums[i] != 0){
arr[n++] = nums[i];
}else{
arr[n++] = 0;
if(n >= len) break;
arr[n++] = 0;
}
}
}
}