lintcode Beginners four

142. O (1) time detecting powers of 2

With O ( . 1 ) time detecting integer  n  whether the  two  power times.

Sample

Example 1:
	Input: 4
	Output: true


Example 2:
	Input:  5
	Output: false

challenge

O (1) time

class Solution:
     "" "
     @param n-: An Integer 
    @return: True or to false 
    " ""
     '' '
     . 1 . using a recursive approach to determining
     two phase approach to determination. 
    For example: 16 and 15 phase, because the 16 bit 01111 10000,15 subtracting 1 bit phase >> 00000 
    or 1 & 0 >> 0 
    power of other than the case where the 2: 
    15 & 14   01111 & 01110   >> 01110 
    '' '
     DEF checkPowerOf2 (Self , the n-): 
        # the Write your code here Wallpaper 
        IF the n-&(n-1) == 0:
            return True
        return false

Square root, square, cube:

1. The square root, int (pow (n, 0.5))

2. squared, pow (n, 2)

3. cubed, pow (n, 3)

 

Guess you like

Origin www.cnblogs.com/yunxintryyoubest/p/12195522.html