[Title] array of daily brush

Topic one:

  Given an array nums and a value val, you need to place the elements to remove all equal values val, and returns the new length of the array after removal.
  Do not use extra space for an array, you must modify the input array in place and completed under the conditions of use O (1) extra space.
  Order of the elements may be changed. You do not need to consider beyond the elements of the new array length behind.
  

    Example:
  Given nums = [0,1,2,2,3,0,4,2], val = 2,
  the function should return a new length of 5, and the first five elements nums is 0, 1, 3 , 0, 4.
  Note that these five elements can be in any order.
  You do not need to consider beyond the elements of the new array length behind.

 PS: Do not confuse the method name

  // useless 
   public  int removeElement ( int [] the nums, int Val) {
         for ( int I = 0; I <nums.length; I ++ ) {
             IF (the nums [I] == Val) {
 //               silly thought there remove method, it can be seen by way of example do not need to remove off
 //               nums.remove (the nums [I]); 
            } 
        } 
        return nums.length; 
    } 
    
    public  int removeElement2 ( int [] the nums, int Val) {
         int I = 0 ;
         for ( int0 = J; J <nums.length; J ++ ) {
             IF (the nums [J] =! Val) { 
                the nums [I] = the nums [J]; 
                I ++; // different from the next question, which is the index assigned from 0 
            } 
        } 
        return I; 
    }

 

Topic two:

  Given a sorted array , you need to place delete elements are repeated so that each element appears only once, after returning the new length of the array is removed.
  Do not use extra space for an array, you must modify the input array in place and completed under the conditions of use O (1) extra space.

    / ** 
     * This is too slow it 
        executes with: 984 ms, beat all Java submission of 5.01% of user 
        memory consumption: 42.9 MB, beat the 5.02% of all users in Java submission 
     * @param nums 
     * @return 
     * / 
    public  int RemoveDuplicates ( int [] the nums) { 
        String STR = ";" ;
         int J = 0 ;
         for ( int I = 0; I <nums.length; I ++ ) {
             // to wrap front herein, or a -3, 3 will be considered to have the presence of the 
            IF (str.indexOf ( ";" the nums + [I] + ";")> -1 ) { 
            } the else {
                STRThe nums = + [I] + ";" ; 
                the nums [J] = the nums [I]; 
                J ++ ; 
            } 
        } 
        return J; 
    } 
    
    / ** 
     * title ordered array is described, nor is it true remove remove 
     * @param the nums 
     * @return 
     * / 
    public  int removeDuplicates2 ( int [] the nums) {
         int I = 0 ;
         for ( int J =. 1; J <nums.length; J ++ ) {
             // unequal to the host 
            IF (the nums [ J]! =the nums [I]) {
                I ++; // index number 0 is the first direct 
                the nums [I] = the nums [J]; 
            } 
        } 
        return ++ I; // not ++ I 
     }

 

Guess you like

Origin www.cnblogs.com/utomboy/p/12402698.html