1. class Solution {
  2. public:
  3. bool hasAlternatingBits(int n) {
  4. int last = -;
  5. while (n)
  6. {
  7. int x = n & ;
  8. if (last == -)
  9. {
  10. last = x;
  11. }
  12. else
  13. {
  14. if (x == last)
  15. {
  16. return false;
  17. }
  18. else
  19. {
  20. last = x;
  21. }
  22. }
  23. n >>= ;//n右移1位
  24. }
  25. return true;
  26. }
  27. };

leetcode693的更多相关文章

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

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

  2. leetcode693:Binary Number with Alternating Bits

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

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

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

随机推荐

  1. Ceph与OpenStack整合(仅为云主机提供云盘功能)

    1. Ceph与OpenStack整合(仅为云主机提供云盘功能) 创建: linhaifeng,最新修改: 大约1分钟以前 ceph ceph osd pool create volumes 128 ...

  2. LeetCode OJ:Three Sum(三数之和)

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...

  3. OC-SEL 和 Class

    [认识选择器]============================================ SEL 1.选择器(SEL)是一个变量类型. 2.选择器用来装成员消息(成员方法) people ...

  4. CoreData之增删改查

    1. 导入库文件CoreData.framework2. 在iOS的Core Data 中建Data Model文件 此时有三种选择 2.1. 选Data Model(如默认名Model.xcdata ...

  5. <input type="checkbox" name="" value=" ">

    <input type="checkbox"  name=""  value=" ">

  6. New Concept English three (42)

    21 33 Cave exploration, or pot-holing, as it has come to be known, is a relatively new sport. Perhap ...

  7. opencv roi resize 会导致内存拷贝产生子图像

    opencv roi区域 resize之后,roi的引用已不是原图的引用,而是内存拷贝产生的子图像. http://blog.csdn.net/qianqing13579/article/detail ...

  8. Java进阶知识点4:不可变对象与并发 - 从String说起

    一.String的不可变特性 熟悉Java的朋友都知道,Java中的String有一个很特别的特性,就是你会发现无论你调用String的什么方法,均无法修改this对象的状态.当确实需要修改Strin ...

  9. HDU - 6096 :String (AC自动机,已知前后缀,匹配单词,弱数据)

    Bob has a dictionary with N words in it. Now there is a list of words in which the middle part of th ...

  10. C# 通过窗口句柄获取程序路径 图标

    转自:http://qqhack8.blog.163.com/blog/static/11414798520113363829505/ C# 通过窗口句柄获取程序路径 图标using System;u ...