LeetCode笔记:693. Binary Number with Alternating Bits

LeetCode笔记:693. Binary Number with Alternating Bits

96 Cloudox_ 关注

2018.01.13 10:20 字数 468 阅读 16评论 0喜欢 0

问题(Easy):

Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values.

Example 1:
Input: 5
Output: True
Explanation:
The binary representation of 5 is: 101

Example 2:
Input: 7
Output: False
Explanation:
The binary representation of 7 is: 111.

Example 3:
Input: 11
Output: False
Explanation:
The binary representation of 11 is: 1011.

Example 4:
Input: 10
Output: True
Explanation:
The binary representation of 10 is: 1010.

大意:

给出一个正整数,检查其是否有交替的比特位:也就是说,如果相邻的两个比特位始终有不同的值。

例1:
输入:5
输出:True
解释:
5的二进制表示是:101

例2:
输入:7
输出:False
解释:
5的二进制表示是:111

例3:
输入:11
输出:False
解释:
5的二进制表示是:1011

例4:
输入:10
输出:True
解释:
5的二进制表示是:1010

思路:

最简单的就是循环右移并用模2来获得最低位的值,并依次进行比较看是否不同。

代码(C++):

class Solution {
public:
    bool hasAlternatingBits(int n) {
        int temp = n % 2;
        while (n > 0) {
            n = n >> 1;
            if (n % 2 == temp) return false;
            temp = n % 2;
        }
        return true;
    }
};

他山之石:

交替的比特位其实是个很有特点的排列,如果右移一次,得到的数可以刚好和原本的数互补,比如101010,右移得到10101:

101010
010101

相加就会得到:

11111

我们可以利用这个特点,如果是交替的比特位,右移一位后相加可以得到一个全是1的数,再加1会的得到一个最高位为1,其余位为0的数,与全是1的数相与将为0,但如果不是交替的比特位,就没有这个特性,因此可以这样判断,速度更快。

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

合集:https://github.com/Cloudox/LeetCode-Record

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值