# Code language: Python classSolution: defduplicateZeros(self, arr: List[int]) -> None: """ Do not return anything, modify arr in-place instead. """ n = len(arr) st = [0for _ in arr] i, j = 0, 0 while i < n: st[i] = arr[j] if arr[j] == 0: i += 1 i += 1 j += 1 for i inrange(n): arr[i] = st[i]
1 2 3 4 5 6 7 8 9 10 11 12
// Code language: Java classSolution { publicvoidduplicateZeros(int[] arr) { intn= arr.length; int[] cp = arr.clone(); for (inti=0, j = 0; i < n; ++j) { cp[i++] = arr[j]; if (arr[j] == 0 && i < n) cp[i++] = 0; } for (inti=0; i < n; ++i) arr[i] = cp[i]; } }
1 2 3 4 5 6 7 8 9 10 11 12 13
// Code language: C++ classSolution { public: voidduplicateZeros(vector<int>& arr){ int n = arr.size(); vector<int> cp(arr); for (int i = 0, j = 0; i < n; ++j) { cp[i++] = arr[j]; if (arr[j] == 0 && i < n) cp[i++] = 0; } for (int i = 0; i < n; ++i) arr[i] = cp[i]; } };