231. Power of Two (判断一个数是否是2的幂)

左手的ㄟ右手 2022-07-15 01:26 256阅读 0赞

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

  1. public class Solution {
  2. public boolean isPowerOfTwo(int n) {
  3. return n>0&&Integer.bitCount(n)==1;//Integer.bitCount(-2147483648) is 1
  4. }
  5. }

发表评论

表情:
评论列表 (有 0 条评论,256人围观)

还没有评论,来说两句吧...

相关阅读