231. Power of Two

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了231. Power of Two相关的知识,希望对你有一定的参考价值。

鏍囩锛?/p>

棰樼洰锛?/strong>

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

绛旀锛?/p>

2鐨勫箓鏈変袱涓壒鐐癸細

  1锛夊ぇ浜?

  2锛変簩杩涘埗褰㈠紡閮芥槸棣栦綅涓?锛屽叾浣欎綅涓?锛屽洜姝&(n-1)绛変簬0

1 class Solution {
2 public:
3     bool isPowerOfTwo(int n) {
4         if(  n>0&&(!(n&(n-1)))  )
5         return true;
6         else
7         return false;
8     }
9 };

 

以上是关于231. Power of Two的主要内容,如果未能解决你的问题,请参考以下文章