Given an integer, write a function to determine if it is a power of two.
class Solution { public: bool isPowerOfTwo(int n) { int num = 0; while (n > 0) { if (n &1) num++; n/=2; } if (num == 1) return true; return false; } };
时间: 2024-10-08 04:35:45
Given an integer, write a function to determine if it is a power of two.
class Solution { public: bool isPowerOfTwo(int n) { int num = 0; while (n > 0) { if (n &1) num++; n/=2; } if (num == 1) return true; return false; } };