判断一个数字的二进制形式是不是01交替的。
如5=101,返回True
如7=111,返回False

这道题可以用位运算来实现。看到01交替,就想到移位运算。如果n是01交替的,移位之后进行异或,则得到的数字x各个位都是1。也就是x=n^(n>>1)
剩下的任务就是判断一个数字是否是2的幂减一。也就是((x+1)&x)==0

leetcode693:Binary Number with Alternating Bits的更多相关文章

  1. Leetcode693.Binary Number with Alternating Bits交替位二进制数

    给定一个正整数,检查他是否为交替位二进制数:换句话说,就是他的二进制数相邻的两个位数永不相等. 示例 1: 输入: 5 输出: True 解释: 5的二进制数是: 101 示例 2: 输入: 7 输出 ...

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

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

  3. 693. Binary Number with Alternating Bits - LeetCode

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

  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. 987. Binary Number with Alternating Bits

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

  7. [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 ...

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

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

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

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

随机推荐

  1. Win8 下配置Java开发环境

    背景: 大学期间学习过一段时间的JavaEE.不算很熟悉. 后来学习并在工作中很多其它是iOS开发,iOS的水平属于中上. 对技术已经有一定熟知程度. 近期为了写一些东西,须要用到Java写后台. 流 ...

  2. 《软件开发与创新:ThoughtWorks文集:续集》

    <软件开发与创新:ThoughtWorks文集:续集> 基本信息 原书名:The thoughtWorks anthology, volume 2:More essays on softw ...

  3. [Swift] Swift3.0--GCD

    reference to : http://www.jianshu.com/p/4c983388dca6 估计现在好多人在为这一块头疼,所以先来点干货. //最常用模板 //全局队列异步执行 Disp ...

  4. Java读取properties配置文件经常用法

    在开发中对properties文件的操作还是蛮常常的.所以总结了几种操作方法,为后面的开发能够进行參考. 1.通过java.util.ResourceBundle类来读取 这边測试用到了枚举类进行传入 ...

  5. std::tr1::function

    转自:https://www.cnblogs.com/qlee/archive/2011/07/04/2097594.html 在C++的TR1中(Technology Report)中包含一个fun ...

  6. Python并发编程-Redis

    Redis 简介 Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库. Remote Dictionary Server(Redis)是一个基于 key-value ...

  7. 线程 Timer TimerTask 计时器 定时任务 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  8. C# WCF 完整实例,winform 窗体作为 宿主

    上一次提到,我们的WCF程序宿主是发布到IIS上面的.虽然这样做未尝不可,不过不便于我们进行“开始”或“停止”WCF服务的操作.所以再次尝试了编写以窗体应用程序作为WCF服务宿主的方式,并取得了成功. ...

  9. Golang 中三种读取文件发放性能对比

    Golang 中读取文件大概有三种方法,分别为: 1. 通过原生态 io 包中的 read 方法进行读取 2. 通过 io/ioutil 包提供的 read 方法进行读取 3. 通过 bufio 包提 ...

  10. [Javascirpt] Immediately-Invoked function!!! IMPORTANT

    var parkRides = [["Birch Bumpers", 40], ["Pines Plunge", 55], ["Cedar Coast ...