LeetCode 231. Power of Two

    xiaoxiao2021-03-25  131

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

    思路: 判断一个数是否是2的幂 2的N次幂的特点:仅有首位为1,其余各位都为0 代码:

    class Solution { public: bool isPowerOfTwo(int n) { return (n>0) && (!(n&(n-1))); } };
    转载请注明原文地址: https://ju.6miu.com/read-8854.html

    最新回复(0)