class Solution {
public:
bool hasAlternatingBits(int n) {
int last = -;
while (n)
{
int x = n & ;
if (last == -)
{
last = x;
}
else
{
if (x == last)
{
return false;
}
else
{
last = x;
}
}
n >>= ;//n右移1位
}
return true;
}
};

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. opencv:傅里叶变换

    示例代码: #include <opencv.hpp> #include <iostream> using namespace std; using namespace cv; ...

  2. python基础之模块一

    一 time模块 时间表示形式 在Python中,通常有这三种方式来表示时间:时间戳.元组(struct_time).格式化的时间字符串:(1)时间戳(timestamp) :通常来说,时间戳表示的是 ...

  3. LeetCode OJ:Spiral Matrix(螺旋矩阵)

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  4. python学习网址

    http://kuanghy.github.io/categories/#Python

  5. SpringMVC札集(02)——SpringMVC入门完整详细示例(下)

    自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...

  6. 使用 nvm 管理多版本 node

    首先,使用下面的命令来安装 nvm $ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | b ...

  7. mac 电脑学习笔记 -

    新买了个mac mini,第一次用mac,有点linux基础,借此机会记录一下自己的学习过程. 1.个人设置文件 .profile export LS_OPTIONS='--color=auto' # ...

  8. 【转载】 用 Windows API “GetAdaptersInfo” 获取 MAC 时遇到的问题

    From:http://blog.csdn.net/weiyumingwww/article/details/17554461 前段时间有个项目需要获取客户端的 MAC 地址,用作统计去重的参考数据. ...

  9. HTTP Status 500 - Error instantiating servlet class XXXX

    问题描述 web项目中请求出现错误,如下:  HTTP Status 500 - Error instantiating servlet class XXXX类  type Exception rep ...

  10. 排序算法总结(C#版)

    算法质量的衡量标准: 1:时间复杂度:分析关键字比较次数和记录的移动次数: 2:空间复杂度:需要的辅助内存: 3:稳定性:相同的关键字计算后,次序是否不变. 简单排序方法 .直接插入排序 直接插入排序 ...