mrlonelyjtr's blog

写BUG的程序猿


  • Home

  • Categories

  • Archives

  • Tags

  • Search

[LeetCode] Problem 231 - Power of Two

Posted on 2019-07-19 | In Algorithm , LeetCode

Given an integer, write a function to determine if it is a power of two.

Example

No.1

Input: 1

Output: true

Explanation: 2^0 = 1

No.2

Input: 16

Output: true

Explanation: 2^4 = 16

No.3

Input: 218

Output: false

Code

1
2
3
4
5
6
public boolean isPowerOfTwo(int n) {
if (n <= 0)
return false;

return (n & (n - 1)) == 0;
}
# Algorithm # OJ
[LeetCode] Problem 412 - Fizz Buzz
[LeetCode] Problem 326 - Power of Three
  • Table of Contents
  • Overview
mrlonelyjtr

mrlonelyjtr

间歇性踌躇满志,持续性混吃等死

780 posts
18 categories
18 tags
Github
  1. 1. Example
    1. 1.1. No.1
    2. 1.2. No.2
    3. 1.3. No.3
  2. 2. Code
© 2020 mrlonelyjtr
Powered by Hexo
Theme - NexT.Pisces