fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void moveZeroes(int[] nums) {
  11. // A "source" and "destination" pointer: you will read elements from the "source", write them to the "destination".
  12. for (int src = 0, dst = 0; src < nums.length; ++src) {
  13. if (nums[src] != 0) {
  14. nums[dst++] = nums[src];
  15. nums[src] = 0;
  16. }
  17. }
  18. }
  19.  
  20. public static void main (String[] args) throws java.lang.Exception
  21. {
  22. int[] input = {0,1,0,3,12};
  23. System.out.println(Arrays.toString(input));
  24. moveZeroes(input);
  25. System.out.println(Arrays.toString(input));
  26. }
  27. }
Success #stdin #stdout 0.08s 37704KB
stdin
Standard input is empty
stdout
[0, 1, 0, 3, 12]
[1, 3, 12, 0, 0]