Algorithm-finds the smallest number in an integer consisting of the same digit as the target number in a number larger than that number

Source: Internet
Author: User

Topic:

Give 1 positive integers, and find the smallest number in an integer larger than this number in the digit of the same number. For example: 12352874 of the result is 12354278

Analysis:

The objective of this topic is to find and sort arrays.

Of course, the premise is that you have to understand what the idea of this topic looks like. Convert a positive integer to char array A, the length n, the end character is a[n-1], and then reverse the array to find, you will find that the first a[n-1] than the small character is a[i], and then the character A[i] and a[n-1] Exchange, the last array of I to n-1 part of the upgrade sort, you will find , it is the final result that is the smallest number in a number set that is larger than the target integer.

The analysis process for the numbers in the example is:

[1, 2, 3, 5, 2, 8, 7, 4]

^

[1, 2, 3, 5, 2, 8, 7, 4]

^ (2 < 4, swap)

[1, 2, 3, 5, 4, 8, 7, 2]

^, and then the next three-bit character upgrade sort, the result is:

[1, 2, 3, 5, 4, 2, 7, 8]

Again, this is followed by the following results:

[1, 2, 3, 5, 4, 2, 8, 7]

[1, 2, 3, 5, 4, 7, 2, 8]

[1, 2, 3, 5, 4, 7, 8, 2]

[2, 1, 2, 3, 4, 5, 7, 8]

......

Thus, the Java code of the above idea is implemented as:

     Public intFindceil (intsrc) {        Char[] chars =string.valueof (SRC). ToCharArray (); intindex =-1; intLastIndex = chars.length-1; CharLastchar =Chars[lastindex];  for(inti = chars.length-2; i >-1; i--) {            if(Chars[i] <Lastchar) {Index=i;  Break; }        }        if(Index >-1) {            CharCH =Chars[index]; Chars[index]=Lastchar; Chars[lastindex]=ch; QuickSort (chars, index+ 1, LastIndex); }        returnInteger.parseint (string.valueof (chars)); }

Then, the algorithm for fast sorting the char array is as follows:

     Public voidQuickSort (Char[] chars,intStartintend) {        if(Chars! =NULL&& Start >-1 && End < Chars.length && Start <end) {            inti = start, j =end; CharValue =Chars[start];  while(I <j) { while(J > Start && chars[j] >=value) {J--; }                if(I <j) {Chars[i]=Chars[j]; I++; }                 while(I < end && Chars[i] <value) {i++; }                if(I <j) {Chars[j]=Chars[i]; J--; } Chars[i]=value;                QuickSort (chars, start, i); QuickSort (chars, I+ 1, end); }        }    }

Then, the problem is the perfect solution!

Algorithm-finds the smallest number in an integer consisting of the same digit as the target number in a number larger than that number

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.