Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

231. Power of Two #202

Open
Tcdian opened this issue Jun 8, 2020 · 1 comment
Open

231. Power of Two #202

Tcdian opened this issue Jun 8, 2020 · 1 comment

Comments

@Tcdian
Copy link
Owner

Tcdian commented Jun 8, 2020

231. Power of Two

给定一个整数,编写一个函数来判断它是否是 2 的幂次方。

Example 1

Input: 1
Output: true 
Explanation: 20 = 1

Example 2

Input: 16
Output: true
Explanation: 24 = 16

Example 3

Input: 218
Output: false

@Tcdian
Copy link
Owner Author

Tcdian commented Jun 8, 2020

Solution

  • JavaScript Solution
/**
 * @param {number} n
 * @return {boolean}
 */
var isPowerOfTwo = function(n) {
    if (n <= 0) {
        return false;
    }
    return (n & (n - 1)) === 0;
};
  • TypeScript Solution
function isPowerOfTwo(n: number): boolean {
    if (n <= 0) {
        return false;
    }
    return (n & (n - 1)) === 0;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant