Array element move forward question (today's headline written question)

Source: Internet
Author: User

Problem Description: Given an array of integers and an integer x, the elements in the array whose value is X are placed in front of the array, and the other elements are in the same sequence.

For example: The original array is {4,0,1,0,2,,3,6,0,5},x=0, then the adjusted array is {0,0,0,4,1,2,3,6,5}

Analysis: There are many solutions to this problem.

Solution One: auxiliary array method. Creates an array of the same length as the original array, starting at the end of the original array, and ignoring if the element value is x.

Otherwise, the element is copied to the end of the new array. After the original array is scanned, all elements that are not re-assigned to the new array are assigned the value x.

Finally, all the elements of the new array are assigned to the original array.

Assuming the length of the original array is n, the space complexity of the algorithm is O (n) and the time complexity is O (n).

Solution Two: a bit similar to the solution, set a pointer j is used to store the element value of the subscript x, the initial set J is the subscript of the last element of the array, and then start scanning from the tail,

If the element is not X, if J refers to an element x, the value of the two element is exchanged, the last j--, and if the element is x, the loop is ignored.

The method does not require additional auxiliary space, the space complexity is O (1), the time complexity is O (n).

Because the solution is relatively simple, I will not write the specific code, here I give the solution of two Java code implementation.

1 ImportJava.util.*;2  Public classMain {3      Public Static voidAdjustintA[],intx) {4         intI,j=a.length-1;//J is the subscript for the element value x5          for(i=a.length-1;i>=0;i--)6         {7             if(a[i]!=x)8             {9                 if(a[j]==x)Ten                 { One                     intT=a[i];//swap the position of two elements Aa[i]=A[j]; -a[j]=T; -                 } thej--; -             } -         } -     } +      -      Public Static voidMain (string[] args) { +         //TODO Auto-generated method stubs AScanner scan=NewScanner (system.in); at         inta[]={4,0,1,0,2,3,6,0,5}; -          for(inti=0;i<a.length;i++) -         { -System.out.print (a[i]+ ","); -         } - System.out.println (); inSystem.out.print ("Please enter the element value to be advanced:"); -         intx=scan.nextint (); to adjust (a,x); +          for(inti=0;i<a.length;i++) -         { theSystem.out.print (a[i]+ ","); *         } $         Panax Notoginseng  -     } the  +}

The test sample output is:

4,0,1,0,2,3,6,0,5,
Please enter the element value to advance: 0
0,0,0,4,1,2,3,6,5,

Extrapolate: The topic is forward, also has a back shift, the reader can try to implement their own programming.

Array element move forward question (today's headline written question)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.