Leetcode 231: Power of Two

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

 

 1 public class Solution {
 2     public bool IsPowerOfTwo(int n) {
 3         if (n <= 0) return false;
 4         if (n <= 2) return true;
 5         
 6         while (n > 2)
 7         {
 8             if (n % 2 != 0) return false;
 9             n = n / 2;
10         }
11         
12         return true;
13     }
14 }

 

posted @ 2017-12-03 11:29  逸朵  阅读(101)  评论(0)    收藏  举报