Comic: What is the dictionary order algorithm?

Comic: What is the dictionary order algorithm?

Click on the "Programmer Xiaohui" above, and select the "Top Official Account" to
deliver interesting and meaningful articles as soon as possible!

Comic: What is the dictionary order algorithm?
Comic: What is the dictionary order algorithm?

----- the next day-----
Comic: What is the dictionary order algorithm?

Comic: What is the dictionary order algorithm?
Comic: What is the dictionary order algorithm?

Algorithm topic:

Given a positive integer, implement a method to find the nearest "transpose number" to the integer greater than itself.

What is transposition? It is to arrange all the digits of an integer to obtain a new integer. For example, 53241 and 23541.

Xiao Hui doesn't know how to call this kind of transposed integer, so let's call it "transposed number".

The title requires writing a method to find the nearest transposition greater than itself. For example:

Enter 12345, return 12354,
enter 12354, return 12435,
enter 12435, return 12453
Comic: What is the dictionary order algorithm?

Comic: What is the dictionary order algorithm?

The "rules" discovered by Xiao Hui:

Enter 12345 and return 12354
12354-12345 = 9
is exactly the power of 9

Enter 12354 and return 12435
12435-12354 = 81 which
is exactly the square of 9

So, every time you calculate the nearest transposition, you only need to add 9 to the Nth power?

Comic: What is the dictionary order algorithm?
Comic: What is the dictionary order algorithm?
Comic: What is the dictionary order algorithm?
Comic: What is the dictionary order algorithm?

————————————
Comic: What is the dictionary order algorithm?

Comic: What is the dictionary order algorithm?
Comic: What is the dictionary order algorithm?
Comic: What is the dictionary order algorithm?

Comic: What is the dictionary order algorithm?

Comic: What is the dictionary order algorithm?

Give a chestnut:

Given the numbers 1, 2, 3, 4, and 5.
The largest combination: 54321 The
smallest combination: 12345

Comic: What is the dictionary order algorithm?

For example, given the integer 12354, how to find the transposition nearest to it and greater than it?

In order to be close to the original number, we need to keep the high order unchanged and the low order to change the order within the smallest range.

So, how many bits need to be converted? This depends on the reverse order area of ​​the current integer.
Comic: What is the dictionary order algorithm?

If shown, the reverse order area of ​​12354 is the last two digits, just look at the current maximum combination of these two digits. If you want to be the closest to the original number and larger than the original number, you must change from the third to last.

How to change it? The last 3 digit of 12345 is 3, we need to find the number just greater than 3 from the reverse order area, and exchange it with the position of 3:
Comic: What is the dictionary order algorithm?

The temporary result after the swap is 12453, and the last three digits have been determined. At this time, the last two digits are still in reverse order. We need to convert the last two digits back to order to ensure that the last two digits are as small as possible when the third-to-last digit value is 4:

Comic: What is the dictionary order algorithm?

In this way, we get the desired result 12435.
Comic: What is the dictionary order algorithm?
Comic: What is the dictionary order algorithm?

//主流程,返回最近一个大于自身的相同数字组成的整数。
public static int[] findNearestNumber(int[] numbers){
   //拷贝入参,避免直接修改入参
   int[] numbersCopy = Arrays.copyOf(numbers, numbers.length);
   //1.从后向前查看逆序区域,找到逆序区域的前一位,也就是数字置换的边界
   int index = findTransferPoint(numbersCopy);
   //如果数字置换边界是0,说明整个数组已经逆序,无法得到更大的相同数字组成的整数,返回自身
   if(index == 0){
       return null;
   }
   //2.把逆序区域的前一位和逆序区域中刚刚大于它的数字交换位置
   exchangeHead(numbersCopy, index);
   //3.把原来的逆序区域转为顺序
   reverse(numbersCopy, index);
   return numbersCopy;
}
private static int findTransferPoint(int[] numbers){
   for(int i=numbers.length-1; i>0; i--){
       if(numbers[i] > numbers[i-1]){
          return i;
       }
   }
   return 0;
}
private  static int[] exchangeHead(int[] numbers, int index){
   int head = numbers[index-1];
   for(int i=numbers.length-1; i>0; i--){
       if(head < numbers[i]){
           numbers[index-1] =  numbers[i];
           numbers[i] = head;
           break;
       }
   }
   return numbers;
}

private static int[] reverse(int[] num, int index){
   for(int i=index,j=num.length-1; i<j; i++,j--){
       int temp = num[i];
       num[i] = num[j];
       num[j] = temp;
   }
   return num;
}
public static void main(String[] args) {
   int[] numbers = {1,2,3,4,5};
   for(int i=0; i<10;i++){
       numbers = findNearestNumber(numbers);
       outputNumbers(numbers);
   }
}
//输出数组
privte static void outputNumbers(int[] numbers){
   for(int i : numbers){
      System.out.print(i);
   }
   System.out.println();

Three steps to get the nearest transposition:

1. Look at the reverse order area from back to front, and find the previous bit of the reverse order area, which is the boundary of the number replacement.
2. Exchange the previous bit of the reverse order area with the number just greater than it in the reverse order area
3. Reverse the original order Region to sequence
Comic: What is the dictionary order algorithm?

This solution has a tall name: dictionary order algorithm.
Comic: What is the dictionary order algorithm?

Comic: What is the dictionary order algorithm?

Comic: What is the dictionary order algorithm?

A few additions:

This comic is purely entertainment. Please cherish your current work as much as possible and do not imitate Xiao Hui's behavior.

—————END—————
Comic: What is the dictionary order algorithm?

Friends who like this article, please press and hold the picture to follow the subscription account programmer Xiaohui, and watch more exciting content

Comic: What is the dictionary order algorithm?

Guess you like

Origin blog.51cto.com/14982143/2550754