Description:
Given an integer, write a function to determine if it is a power of two.
public class Solution { public boolean isPowerOfTwo(int n) { boolean flag = false; if(n == 0) return false; while(true) { if(n == 1) { flag = true; break; } if(n % 2 != 0) { flag = false; break; } else { n /= 2; } } return flag; } }
时间: 2024-11-05 12:18:50