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

Example 1:

Input: 5
Output: True
Explanation:
The binary representation of 5 is: 101

Example 2:

Input: 7
Output: False
Explanation:
The binary representation of 7 is: 111.

Example 3:

Input: 11
Output: False
Explanation:
The binary representation of 11 is: 1011.

Example 4:

Input: 10
Output: True
Explanation:
The binary representation of 10 is: 1010.
 
class Solution:
def hasAlternatingBits(self, n):
"""
:type n: int
:rtype: bool
"""
b=bin(n)[2:] l=len(b)
flag=True for i in range(l):
if i!=l-1:
if b[i]==b[i+1]:
flag=False
break return flag

  

[LeetCode&Python] Problem 693. Binary Number with Alternating Bits的更多相关文章

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

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

  2. 693. Binary Number with Alternating Bits - LeetCode

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

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

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

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

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

  5. [LeetCode&Python] Problem 762. Prime Number of Set Bits in Binary Representation

    Given two integers L and R, find the count of numbers in the range [L, R](inclusive) having a prime ...

  6. 693. Binary Number with Alternating Bits

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

  7. [LeetCode] 693. Binary Number with Alternating Bits_Easy

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

  8. [LeetCode] Binary Number with Alternating Bits 有交替位的二进制数

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

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

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

随机推荐

  1. 汇编语言调用Linux系统调用read和write

    .section .data UserMsg: .ascii "Please input the message:" LenOfUserMsg: .equ lenMsg, LenO ...

  2. STL_算法_02_排序算法

    ◆ 常用的排序算法: 1.1.合并(容器A(全部/部分)&容器B(全部/部分)==>容器C(全部/部分),容器C中元素已经排好顺序),返回的值==>iteratorOutBegin ...

  3. Ubuntu安装NVIDA显卡驱动

    0. 综述 电脑型号:R720 Ubuntu版本:16 显卡型号:1050ti 目前,知道3种安装N卡驱动的方法: 1. PPA源:最简便,但未必有最新驱动(亲测),或可能遇到问题(风闻). sudo ...

  4. Travelling Salesman and Special Numbers CodeForces - 914C (数位dp)

    大意: 对于一个数$x$, 每次操作可将$x$变为$x$二进制中1的个数 定义经过k次操作变为1的数为好数, 求$[1,n]$中有多少个好数 注意到n二进制位最大1000位, 经过一次操作后一定变为1 ...

  5. Docker 构建 redis 集群

    安装docker 1.yum install docker 方法一: 1. docker pull redis 2.docker run -d --name redis-1 -p 7001:6379 ...

  6. Oracle11g温习-第一章 2、ORACLE 物理结构

    2013年4月27日 星期六 10:26 物理操作系统文件的集合.主要包括: 控制文件(参数文件init$ORACLE_SID.ora记录了控制文件的位置) 二进制文件,控制文件由参数control_ ...

  7. Leetcode 90

    // 重复元素在去重的时候会出现顺序不同去不了重,这时候需要对add进行排序class Solution { public: vector<vector<int>> subse ...

  8. 硬盘分区表知识——详解硬盘MBR (转)

    Ref: http://www.blogjava.net/galaxyp/archive/2010/04/25/319344.html 硬盘是现在计算机上最常用的存储器之一.我们都知道,计算机之所以神 ...

  9. log4j配置文件位置详解

    自动加载配置文件: (1)如果采用log4j输出日志,要对log4j加载配置文件的过程有所了解.log4j启动时,默认会寻找source folder下的log4j.xml配置文件,若没有,会寻找lo ...

  10. iOS UI-AlertView(警示框)和ActionSheet(选择框、操作表单)

    #import "ViewController.h" @interface ViewController ()<UIAlertViewDelegate,UIActionShe ...