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

示例 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. Windows API 第20篇 SetVolumeMountPoint 设置卷挂载点参数错误

    函数原型:BOOL SetVolumeMountPoint(                                                   IN   LPCTSTR lpszVo ...

  2. React学习整理

    React介绍 React设计思想及其独特,属于革命性创新,性能出众,代码逻辑却非常简单. 库(library):小而巧,库只提供了特定的api.优点是船小好调头,可以很方便的从一个库切换到另外的库, ...

  3. Odoo(OpenERP)配置文件详解

    [options] ; addons模块的查找路径 addons_path = E:\GreenOdoo8.0\source\openerp\addons ; 管理员主控密码(用于创建.还原和备份数据 ...

  4. JZOJ5822 【NOIP提高A组模拟2018.8.16】 量子纠缠

    这是一道很巧妙的题目. 今早,我调了好久,终于将它切掉了-- 题目 Description Input 第一行包含一个正整数 m,代表操作数. 接下来 m 行,每行可能有以下形式: 1 s 代表将数字 ...

  5. CODE[VS]1372:DNA

    Description 为了进一步分析外星生物,专家们决定对 DNA 进行切割.限制性核酸内切酶是基因工程中的重要的工具酶.它会识别一段碱基序列(说白了就是只包含 ATGC 的序列)并且切割开.Eco ...

  6. Django项目:CMDB(服务器硬件资产自动采集系统)--06--06CMDB测试Linux系统采集硬件数据的命令01

    #base.py # ————————01CMDB获取服务器基本信息———————— from config import settings #配置文件 class BasePlugin(object ...

  7. JS中对象转数组方法总结

    1.Array.from() 方法,用于数组的浅拷贝.就是将一个类数组对象或者可遍历对象转换成一个真正的数组.eg: let obj = { 0: 'nihao', 1: 'haha', 2: 'ga ...

  8. IntelliJ IDEA 如何在同一个窗口创建多个项目

    一.IntelliJ IDEA与Eclipse的区别   二.在同一个窗口创建多个项目 1.打开IntelliJ IDEA,点击Create New Project 2.Java Enterprise ...

  9. CentOS 6.5 Apache、MySQL、PHP环境配置(LAMP)

    yum -y install httpd mysql-server php #安装apache.mysql和PHP yum -y install php-mysql php-gd php-mbstri ...

  10. jsp导出的word默认打开是web视图,希望是页面视图

    方法1 ( velocity+java )我也遇到了这个问题,已经解决:1 .<html xmlns:v='urn:schemas-microsoft-com:vml'xmlns:o='urn: ...