给定一个正整数,检查他是否为交替位二进制数:换句话说,就是他的二进制数相邻的两个位数永不相等。

示例 1:

输入: 5 输出: True 解释: 5的二进制数是: 101

示例 2:

输入: 7 输出: False 解释: 7的二进制数是: 111

示例 3:

输入: 11 输出: False 解释: 11的二进制数是: 1011

示例 4:

输入: 10 输出: True 解释: 10的二进制数是: 1010

class Solution {
public:
bool hasAlternatingBits(int n) {
int flag = -1;
while(n)
{
int temp = n % 2;
n /= 2;
if(flag != -1)
{
if(flag == temp)
return false;
}
flag = temp;
}
return true;
}
};

Leetcode693.Binary Number with Alternating Bits交替位二进制数的更多相关文章

  1. leetcode693:Binary Number with Alternating Bits

    判断一个数字的二进制形式是不是01交替的. 如5=101,返回True 如7=111,返回False 这道题可以用位运算来实现.看到01交替,就想到移位运算.如果n是01交替的,移位之后进行异或,则得 ...

  2. 693. Binary Number with Alternating Bits - LeetCode

    Question 693. Binary Number with Alternating Bits Solution 思路:输入一个整数,它的二进制01交替出现,遍历其二进制字符串,下一个与上一个不等 ...

  3. 【Leetcode_easy】693. Binary Number with Alternating Bits

    problem 693. Binary Number with Alternating Bits solution1: class Solution { public: bool hasAlterna ...

  4. [Swift]LeetCode693. 交替位二进制数 | Binary Number with Alternating Bits

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

  5. [LeetCode] Binary Number with Alternating Bits 有交替位的二进制数

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

  6. 【LeetCode】693. Binary Number with Alternating Bits 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历判断 判断是否是交替模式 位运算 日期 题目地址 ...

  7. 987. Binary Number with Alternating Bits

    Description Given a positive integer, check whether it has alternating bits: namely, if two adjacent ...

  8. LeetCode 693 Binary Number with Alternating Bits 解题报告

    题目要求 Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits w ...

  9. [LeetCode&Python] Problem 693. Binary Number with Alternating Bits

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

随机推荐

  1. Fiilter

    过滤器 过滤请求和响应 作用:        自动登录.        统一编码.        过滤关键字        .... Filter是一个接口 编写filter步骤: 1.编写一个类 a ...

  2. Ionic 微信支付

    1.安装插件 ionic plugin add https://github.com/mrwutong/cordova-qdc-wxpay.git 2.代码 controller.js angular ...

  3. Weekly Challenges - Week 11

    一拖再拖,忍无可忍,自己懒的没救了. 一周前就结束的比赛,到现在才想起来补. 最后一题貌似又遇到了splay...还是不会!!!shit 题目链接 A题--快速幂 #include <cmath ...

  4. Delphi 设计模式:《HeadFirst设计模式》Delphi2007代码---组合模式之Menus[转]

     1  2{<HeadFirst设计模式>之组合模式 }  3{ 组合与单项的抽象父类           }  4{ 编译工具:Delphi2007 for win32}  5{ E-M ...

  5. [转载] OpenCV2.4.3 CheatSheet学习(四)

    五.数据的输入和输出 1. 将数据写入YAML(或XML) 注意,在OpenCV中,无论读写,文件的格式均由指定的后缀名确定.示例: FileStorage fs("test.yml&quo ...

  6. Dockerfile镜像制作时间同步

    1.问题描述 宿主机与容器时间相差8小时 2.原因 宿主机采用了CST时区,CST应该是指(China Shanghai Time,东八区时间)容器采用了UTC时区,UTC应该是指(Coordinat ...

  7. 提高scrapy的抓取效率

    增加并发 默认scrapy开启的并发线程的个数是32个,可以适当的进行增加.在settings中进行设置CONCURRENT_REQUESTS=100 降低日志级别 在运行的时候,会有大量的日志信息的 ...

  8. HBase 三维模型解析

    总结下一直想写hbase的实践经验,在用hbase的过程中,我们都知道,rowkey设计的好坏,是我们能最大发挥hbase的架构优势,也是我们是否正确理解hbase的一个关键点.闲话少说,进入正题. ...

  9. Theano入门——CIFAR-10和CIFAR-100数据集

    Theano入门——CIFAR-10和CIFAR-100数据集 1.CIFAR-10数据集介绍 CIFAR-10数据集包含60000个32*32的彩色图像,共有10类.有50000个训练图像和1000 ...

  10. mongodb集群搭建过程记录

    mongodb集群搭建花费比较长的时间,在此记录下过程,方便以后使用 一 软件环境 系统:ubuntu 18.04,mongodb 社区版4.2 https://docs.mongodb.com/ma ...