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 ));
}
}