A minimum integer greater than two adjacent digits.

Source: Internet
Author: User

Question Description: Calculate the smallest integer with two adjacent digits greater than an integer n.
 
Java code
Public class test {
 
Public static void main (String [] args ){
// Int num = (int) (Math. random () * 10000000 );
Int num = 9899;
System. out. println (num );
System. out. println (getResult (num ));
}
 
Private static int getResult (int num ){
Char [] array = String. valueOf (num). toCharArray ();
Int length = array. length;
Int from = 1;
While (from <length ){
// Locate the first location that is repeated with the previous one
If (array [from] = array [from-1]) {
// Roll back, find the first position not 9
While (from> = 0 & array [from] = '9 '){
From --;
}
// If the first digit is 9, the first digit is 1.
If (from <0 ){
Array = ('1' + String. valueOf (num). toCharArray ();
Length ++;
From = 1;
Break;
}
Array [from] = (char) (array [from] + 1 );
// If the modification is different from the previous one
If (array [from]! = Array [from-1]) {
From = from + 1;
Break;
}
} Else {
From ++;
}
}
// If the original number is not repeated, + 1
If (from = length ){
Num ++;
Array = String. valueOf (num). toCharArray ();
Length = array. length;
If (length> 1 & array [length-2] = array [length-1]) {
Return getResult (num );
}
Return num;
}
 
// Starting from the next bit of the changed bit, fill in the 0 and 1 Sequences
Char c = '0 ';
Int s = '0' + '1 ';
For (; from <length; from ++ ){
Array [from] = c;
C = (char) (s-c );
}
 
Return Integer. valueOf (String. valueOf (array ));
}
}

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.