TypechoJoeTheme

IT技术分享

统计

[LeetCode 231] Power of Two [Java] [Runtime : 2MS]

2017-09-01
/
0 评论
/
699 阅读
/
正在检测是否收录...
09/01

1. Description

Implement pow(x, n).

2. Runtime Distribution

3. Submission Details

4. Example

Input 16
Output: True

5. Code

public boolean isPowerOfTwo(int n) {
    long num = n;
    if (n == 0) {
        return false;
    }
    return (num & (num - 1)) == 0;
}

6.Test

public class LeetCode0231 {

    public boolean isPowerOfTwo(int n) {
        long num = n;
        if (n == 0) {
            return false;
        }
        return (num & (num - 1)) == 0;
    }

    public static void main(String[] args) {
        LeetCode0231 leetcode = new LeetCode0231();
        System.out.println(leetcode.isPowerOfTwo(256));
    }

}
BIT
朗读
赞 · 0
版权属于:

IT技术分享

本文链接:

https://idunso.com/archives/774/(转载时请注明本文出处及文章链接)