Leetcode (869)-Reorder to get a power of 2

Source: Internet
Author: User

Starting with a positive integer N , we reorder the numbers in any order (including the original order), noting that their leading digits cannot be zero.

If we can get a power of 2 by the above method, return it true ; false

Example 1:

Input: 1 output: True

Example 2:

Input: 10 Output: false

Example 3:

Input: 16 Output: True

Example 4:

Input: 24 Output: false

Idea: The focus of this question is to find out the full array of numbers (except for the beginning of 0), and whether the number is a power of 2

First, let's summarize the whole arrangement.

This is a C + + function, contained in the header file <algorithm>, below is the general usage of the basic format.

1  int a[]; 2   Do {3      4 }while (Next_permutation (a,a+n));

The STL provides two functions for calculating the permutations and combinations of relationships, namely Next_permutation and prev_permutation. First we must understand what is the "next" permutation, what is the "previous" permutation combination. Consider a sequence of three characters {a,b,c}. This sequence has six possible permutation combinations: ABC,ACB,BAC,BCA,CAB,CBA. These permutations are sorted according to the Less-than operator in Dictionary order (lexicographical). That is, ABC is ranked first because each element is smaller than the element that follows it. ACB is the second permutation, because it is a new combination that is fixed after a (the smallest element in the sequence). Similarly, the permutations of a fixed B (minor element in a sequence) are arranged in a sequence preceded by a permutation of the fixed C. Take BAC and BCA for example, BAC before BCA, because order AC is less than sequence ca. In the face of BCA, we can say that the previous permutation is a BAC, and then a permutation is a cab. The sequence ABC does not have a "previous" permutation combination, and the CBA does not have a "latter" permutation.

So generally before using the Next_permutation function, the numbers are sorted well, according to the order from small to large.

The following is a detailed analysis of how the Next_permutation function is implemented.

    1. Scan from right to left, find the first number that goes against the incremental trend, called PartitionNumber, which 6 happens to be the partitionnumber we find.
    2. Scanning from right to left, found that the first is larger than the number of PartitionNumber, called ChangeNumber. and 7 happens to be the changenumber we find, we need to note that such a number must be there, otherwise, will not find so It's partitionnumber.
    3. Exchange PartitionNumber and changenumber. Such a step would make the new permutation make up a larger number than the old one, and of course, the increase in the new number is not necessarily the smallest.
    4. Reverses the number on the right side of the PartitionNumber. At this point, the PartitionNumber to the right of the arrangement has been strictly from the large to the small arrangement, after such reversal, it can be ensured that the new arrangement of the number of the increase in the range of the smallest possible.
TEMPLATE&LT;CALSS bidrectionaliterator>BOOLnext_permutation (Bidrectionaliterator first,bidrectionaliterator last) {if(first = = last)return false;/*Empty Interval*/Bidrectionaliterator i=First ; ++i; if(i = = last)return false;/*only one element*/I= Last;/*I point to the tail end*/--i;  for(;;) {Bidrectionaliterator II=i; --i; /*Lock A group of (two) adjacent elements above*/         if(*i < *II)/*if the previous element is less than the next element*/{Bidrectionaliterator J= Last;/*make J point to the tail end*/              while(! (*i < *--j));/*look forward from the tail until it encounters a larger element than the *i.*/Iter_swap (I,J); /*Exchange I,j*/reverse (ii,last); /*rearrange all elements after II in reverse order*/             return true; }         if(i = = first)/*To the front.*/{reverse (first,last); /*rearrange all reverse order*/             return false; }     } }

Then we discuss how to tell if a number is a power of 2.

We know that 1 numbers multiplied by 2 means that the number is shifted to the left by 1 bits, and 2 of the power of 0 is 1, so 2 of the Power of N (2 power n times 0) is to shift 2 left n bits, so we know that if a number n is the power of 1, then it is only the first 2, followed by a number of 1, there must be N & (n-1) 0. (in the 1 number of binary representations of the number of 1 said,n& (n-1) minus the last 1 of N). Therefore, to determine whether a number n is a power of 2, you only need to determine whether n& (N-1) is 0.

So how do you know how many times it's 2?

int log2 (int value)   // recursion determines how many times  A number is 2 if1)   return 0  Else  return1+log2 (value>>1);}

The complete code of the subject is attached below

 BOOLREORDEREDPOWEROF2 (intN) {inti,j,k,tmp; Vector<int>A;      A.clear ();  while(n>0) {a.push_back (n%Ten); N/=Ten;      } sort (A.begin (), A.end ());  Do      {        if(a[0]==0)Continue; TMP=0;  for(i=0; I<a.size (); i++) TMP=tmp*Ten+A[i]; if((tmp&tmp-1)==0)return true; } while(Next_permutation (A.begin (), A.end ())); return false; }

Leetcode (869)-Reorder to get a power of 2

Related Article

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.