Given a positive integer, check whether it has alternating bits: namely if two adjacent bits will always have different values.
Example :
Input: n = 5
Output: true
Explanation: The binary representation of 5 is: 101
Approach:
C++
#include <bits/stdc++.h>using namespace std;string toBinary(int n){string res = "";while (n > 0){if (n % 2 == 0)res = '0' + res;elseres = '1' + res;n = n / 2;}return res;}bool hasAlternatingBits(int n){string str = toBinary(n);int len = str.size();for (int i = 0; i < len - 1; i++){if (str[i] == '0' && str[i + 1] == '0')return false;else if (str[i] == '1' && str[i + 1] == '1')return false;}return true;}int main(){int n = 5;if (hasAlternatingBits(n))cout << "true";elsecout << "false";return 0;}
No comments:
Post a Comment