My Two lines Java Solution using Bit manipulation


  • 0
    W
    class Solution {
        public boolean hasAlternatingBits(int n) {
            int m = n ^ (n >> 1);
            return (m & (m + 1)) == 0;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.