JavaScript algorithm title: To find any 1-9-bit not repeated n-digit number in the combination of the size of the sequence number _javascript tips

Source: Internet
Author: User


The specific topic is this:



Select n numbers from the 1--9, make up the number of n digits that are not duplicated, numbering from small to large, and when you enter any number m, you can find the number corresponding



the number.     such as n=3,m=213. output: [123 (1), 132 (2), 213 (3), 231 (4), 312 (5), 321 (6)]--->x=2



The first thing that comes to mind is to generate an array of all permutations from small to large, then go through the array to get the corresponding ordinal number (array subscript plus 1), or think of a small to large generation push into the array, and then determine whether the number is the current problem, if it is the number of the current array is the length, The better thing than the previous one is that you don't have to waste time calculating the items behind the build. The complexity of the build itself is not high, if extended to 16 or even 36 into the system and give a large number of words is not good, there is a need to waste a part of the space to save data. Maybe we can try other methods that don't have to be built.



We idealize the topic first, and if we give a number n, then M is made up of 1-n n digits (like n=4, which is a combination of 1234 numbers, not other 1349, etc.). The reason why we do this is because we have to simplify the conditions to analyze the common way to solve problems, and to change from a random situation to the ideal situation is not difficult, this article is not verbose. First, analyze the example given by the topic, [123 (1), 132 (2), 213 (3), 231 (4), 312 (5), 321 (6)] 213 in third place, the first number is 2, that is, the first number is 1 in front of him (123,132), to see the second number and the combination of the following number , the first letter 1 is already the smallest, he can not have any number in front, The third number 3 does not need to be looked at, because if the previous number of digits is OK, the last one is only possible, the result is that 213 of the front has 2 (first) + 0 (two-bit) + 0 (tail bit) = 2 number, that is, the current number is in the 3rd position, compared to the answer is true, the other number of analysis is the same. From this we can conclude that we want a function (that is, the SetAll () of the following code) to figure out the total number of probabilities that are smaller than the current number, and then add up to +1 is the desired result, see Code implementation:


// Function function: get each bit, if it is another number, the total number of possibilities is smaller than the current one
// a serial number of the current number (from small to large)
// n Total number of current numbers
function getAll (a, n) {
 var sum = 1; // total
 for (var i = n; i> 1; i-) sum = sum * i; // Calculate the total number of possibilities for n ordered positions and n different numbers
 return sum * (a-1) / n; // Calculate the total number of possibilities that are smaller than the current number than the first a
}

// m sequence of numbers to be calculated
// a stores the number of the current digit and the number of the digit after it
// For example, the a array of 213 is [2,1,1]; a [0] is 2 because the first 2 of 213 is the second smallest of the three 213 numbers; and a [1] is 1 because of the first 13 1 ranked first in 13
function find (m) {
 m = (m + ""). split (""); // Split the current number into an array to facilitate the calculation of each bit
 var a = new Array (m.length + 1) .join (1) .split (""); // Quickly generate an array with a length of m and a value of 1. The function description of a array is shown in the function header above Comment
 for (var i = 0; i <m.length-1; i ++) {
 for (var j = i + 1; j <m.length; j ++) {
  if (+ m [i]> + m [j]) a [i] ++;
 }
 } // Generate a array
 console.log ("a array:", a);
 for (i = 1, sum = 1; i <m.length; i ++) {
 sum + = getAll (+ a [i-1], m.length-i + 1); // The loop calls getAll to calculate the total number of possibilities for each combination of each bit and the number following it to be smaller than the current combination
 }
 return m + "is the" + sum + "bit in the full array;
}
console.log (find (213)); // Output 3
console.log (find (123)); // Output 1
console.log (find (231)); // Output 4
console.log (find (312)); // Output 5
console.log (find (4321)); // Output 24
console.log (find (21)); // Output 2
console.log (find (1)); // Output 1

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.