LeetCode[231] Power of Two

来源:互联网 发布:金税盘开票软件官网 编辑:程序博客网 时间:2024/06/14 02:54

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

2的幂二进制表示中只有一个1,所以一定满足 n & n - 1 == 0

class Solution {public:bool isPowerOfTwo(int n) {if (n <= 0)return false;return (n & n - 1) == 0;}};

0 0
原创粉丝点击