加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

[Lintcode]142. O(1) Check Power of 2

发布时间:2020-12-14 04:25:25 所属栏目:大数据 来源:网络整理
导读:142. O(1) Check Power of 2 本题难度: Easy Topic: MathBit Manipulation Description Using O(1) time to check whether an integer n is a power of 2. Example Example 1: Input: 4 Output: true Example 2: Input: 5 Output: false Challenge O(1) time

142. O(1) Check Power of 2

  • 本题难度: Easy
  • Topic: Math&Bit Manipulation

    Description

    Using O(1) time to check whether an integer n is a power of 2.

Example
Example 1:
Input: 4
Output: true

Example 2:
Input: 5
Output: false

Challenge
O(1) time

别人的代码 参考野球拳刷刷刷LeetCode LintCode 解题报告

class Solution {
    /*
     * @param n: An integer
     * @return: True or false
     */
    public boolean checkPowerOf2(int n) {
        // write your code here
        return n > 0 && (n & (n - 1)) == 0;
    }
}

思路

没想到合适的方法。不太会做这类的题

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读