231. Power of Two
Input: n = 1
Output: true
Explanation: 20 = 1Input: n = 16
Output: true
Explanation: 24 = 16Input: n = 3
Output: falseInput: n = 4
Output: trueInput: n = 5
Output: falseLast updated
Input: n = 1
Output: true
Explanation: 20 = 1Input: n = 16
Output: true
Explanation: 24 = 16Input: n = 3
Output: falseInput: n = 4
Output: trueInput: n = 5
Output: falseLast updated
public class Solution {
public bool IsPowerOfTwo(int n) {
if(n <= 0)
return false;
char[] charlist = Convert.ToString(n,2).ToCharArray();
var count = 0;
foreach (char elem in charlist){
if(elem == '1'){
count += 1;
}
}
return count == 1 ? true : false;
}
}