@@ -0,0 +1,2018 @@
+//  Copyright 2021 The KeepTry Open Source Project%0A//%0A// Licensed under the Apache License, Version 2.0 (the %22License%22);%0A// you may not use this file except in compliance with the License.%0A// You may obtain a copy of the License at%0A//%0A//     http://www.apache.org/licenses/LICENSE-2.0%0A//%0A// Unless required by applicable law or agreed to in writing, software%0A// distributed under the License is distributed on an %22AS IS%22 BASIS,%0A// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A// See the License for the specific language governing permissions and%0A// limitations under the License.%0A//%0A%0Apackage math;%0A%0Aimport java.util.Arrays;%0A%0Apublic class Leetcode1089DuplicateZeros %7B%0A  /*%0A  duplicate each occurrence of zero, shifting the remaining elements to the right.%0A  Note that elements beyond the length of the original array are not written.%0A  Do the above modifications to the input array in place.%0A%0A  */%0A%0A  public static void duplicateZeros(int%5B%5D arr) %7B%0A    /*%0A    1 %3C= arr.length %3C= 10000%0A    0 %3C= arr%5Bi%5D %3C= 9%0A    */%0A    int N = arr.length;%0A    int j = N - 1; // virtual index of extended array%0A    for (int i = 0; i %3C N; i++) %7B%0A      if (arr%5Bi%5D == 0) j++;%0A    %7D%0A%0A    for (int i = N - 1; i %3E= 0; j--, i--) %7B%0A      if (j %3C N) %7B%0A        arr%5Bj%5D = arr%5Bi%5D;%0A        if (arr%5Bi%5D == 0) arr%5B--j%5D = 0;%0A      %7D else %7B%0A        if (arr%5Bi%5D == 0) %7B%0A          --j; // The assumption 'j %3C arr.length' may be changed now and need save value%0A          if (j %3C N) arr%5Bj%5D = 0; // Note here%0A        %7D%0A      %7D%0A    %7D%0A  %7D%0A%0A  //%0A  public static void duplicateZeros2(int%5B%5D arr) %7B%0A    int N = arr.length;%0A    int j = N - 1; // virtual index of extended array%0A    for (int i = 0; i %3C N; i++) %7B%0A      if (arr%5Bi%5D == 0) j++;%0A    %7D%0A%0A    for (int i = N - 1; i %3E= 0; j--, i--) %7B%0A      if (j %3C N) arr%5Bj%5D = arr%5Bi%5D;%0A      if (arr%5Bi%5D == 0 && --j %3C N) arr%5Bj%5D = 0;%0A    %7D%0A  %7D%0A%0A  public static void main(String%5B%5D args) %7B%0A    duplicateZeros(new int%5B%5D %7B8, 4, 5, 0, 0, 0, 0, 7%7D);%0A    duplicateZeros(new int%5B%5D %7B1, 0, 2, 3, 0, 4, 5, 0%7D);%0A  %7D%0A%7D%0A

1089. Duplicate Zeros