Skip to content

Latest commit

 

History

History
22 lines (19 loc) · 421 Bytes

README.md

File metadata and controls

22 lines (19 loc) · 421 Bytes

Power of Two

Solution 1

Binary representation of a power of two contains exactly one bit turned on.

/**
 * Question   : 231. Power of Two
 * Complexity : Time: O(1) ; Space: O(1)
 * Topics     : Bit
 */
class Solution {
    public boolean isPowerOfTwo(int n) {
			if (input <= 0) {
        return false;
      }
      // Turn off the rightmost set bit
      return (input & (input - 1)) == 0;
    }
}