判断一个数字的二进制形式是不是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. key-value 多线程server的Linux C++实现

    项目需求 整体思路 网络通信 字符解析 数据存储与查询 1 存储管理 2 数据查询 多线程 待改进未实现的想法 GitHub源代码 项目需求 设计一个基于Socket或基于HTTP的server,服务 ...

  2. 启明星会议室预定系统Outlook版开始支持Exchange2013与Office365版

    版本启明星会议室预定系统支持Exchange2013与微软云服务Office365版.(注意:Exchange2007与Exchange2010也适合此版本) 1.安装 首页,安装类似启明星普通的会议 ...

  3. HTTP参数CONNETCTION_TIMEOUT和SO_TIMEOUT区别

    在开发中经常碰到这两个参数,但是之前对它们的真正含义一直比较模糊,今天通过调试程序并且结合官方文档,了解了两者的含义与区别. 参数的定义直接去看官方的文档(httpcore-4.3) org.apac ...

  4. 数据库实例: STOREBOOK > 表空间

    ylbtech-Oracle:数据库实例: STOREBOOK > 表空间 表空间(默认) 1. 表空间(默认)返回顶部 1.1, 1.2, 2. 表空间列表(默认)返回顶部 2.1, SYSA ...

  5. C语言:用二进制方式向文件读写一组数据(fread、fwrite)

    #include<stdio.h> #define SIZE 10 struct student { char name[10]; int num; int age; char addr[ ...

  6. 3D屏保程序:汉诺塔

    学过程序的人一定记得汉诺塔.我们学的第一个程序是HelloWorld,而碰到的第一个坑就是汉诺塔,短短十几行代码,不知花费了多少时间精力去理解.我记得当年的开发环境还是蓝屏的,汉诺塔程序的输出还是一行 ...

  7. socket函数send和recv函数

    转自:http://www.cppblog.com/aaxron/archive/2012/04/27/172891.html 在发送端,一次发送4092个字节,在接收端,一次接收4092个字节,但是 ...

  8. PreparedStatement 使用like 模糊查询

    PreparedStatement 使用like 在使用PreparedStatement进行模糊查询的时候废了一番周折,以前一直都没有注意这个问题. 一般情况下我们进行精确查询,sql语句类似:se ...

  9. Winform中产生验证码图片

    1.创建ValidCode类: public class ValidCode { #region Private Fields private const double PI = 3.14159265 ...

  10. js 获取读取cookie

    // --- 设置cookie function setCookie(sName, sValue, expireHours) {     var cookieString = sName + &quo ...