Power of Two

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

判断一个integer是否为2的幂。

 

 1 public class Solution {
 2     public static boolean isPowerOfTwo(int n) {
 3           double d = n;
 4           
 5           if(d==1){
 6               return true;
 7           }
 8             while(d>=2){
 9                 d = d/2;
10                 if(d==1){
11                     return true;
12                 }
13             }
14             return false;
15         }
16 }

252 ms.

posted @ 2015-08-06 10:39  codingcat  阅读(101)  评论(0编辑  收藏  举报