leetcode-283- moving zero

problem:

 

Package com.example.demo; 

public  class Test283 {
     / ** 
     * 0 in the array to move back to the array, colleagues maintain the relative position of other elements 
     * all non-0 element is moved forwardly 
     * define an index, the index represents the non-zero elements, increments upwardly from 0 
     * 
     * @param the nums
      * / 
    public  void moveZeroes ( int [] the nums) {
         int K = 0 ;
         for ( int I = 0; I <nums.length; I ++ ) {
             IF (the nums [ ! I] = 0 ) { 
                the nums [K ++] = the nums [I]; 
            } 
        } 
        //After traversing the upper completion, the situation value between 0-k of all non-zero elements, it is necessary between the elements are all set to k-len 0 
        for ( int I = K; I <nums.length; I ++ ) { 
            the nums [ I] = 0 ; 
        } 
    } 

    public  static  void main (String [] args) { 
        Test283 T = new new Test283 ();
         int [] ARR = {. 1, 0, 0,. 3,. 4,. 5, 0,. 4 }; 
        T .moveZeroes (ARR); 
        for ( int I: ARR) { 
            of System.out.print (I + "" ); 
        } 
    } 
}

 

Guess you like

Origin www.cnblogs.com/nxzblogs/p/11270583.html