#题解# 2的幂

2的幂

https://www.nowcoder.com/practice/4a04240fd2be4e1287aab4af067f6d8f?tpId=196&rp=1&ru=%2Fexam%2Foj&qru=%2Fexam%2Foj&sourceUrl=%2Fexam%2Foj%3Fpage%3D1%26pageSize%3D50%26search%3D%26tab%3D%25E7%25AE%2597%25E6%25B3%2595%25E7%25AF%2587%26topicId%3D196&difficulty=&judgeStatus=0&tags=591&title=&gioEnter=menu

巧妙地运用了位运算和幂的特性

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param n int整型 
     * @return bool布尔型
     */
    bool poweroftwo(int n) {
      int count = (n & n-1);
      return n > 0 && count == 0;
    }
};
全部评论

相关推荐

不放弃的小鱼干很洒脱:好可爱的离职理由
点赞 评论 收藏
分享
头像
03-20 22:00
重庆大学 Java
适彼乐土:“他们不行再找你” 最后的底牌吗?有点意思
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务