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. 爬虫框架pyspider的使用

    j概要:了解了爬虫的基础知识后,接下来我们来使用框架来写爬虫,用框架会使我们写爬虫更加简单,接下来我们来了解一下,pyspider框架的使用,了解了该框架,妈妈再也不用担心我们的学习了. 前期准备: ...

  2. RabbitMQ入门_10_队列长度限制

    参考资料:https://www.rabbitmq.com/maxlength.html RabbitMQ 有两种方式限制队列长度,第一种是对队列中消息总数进行限制: gordon.study.rab ...

  3. /etc/fstab 文件解析

    首先介绍一下fstab :这个文件描述系统中各种文件系统的信息.一般而言,应用程序仅读取这个文件,而不对它进行写操作.对它的维护是系统管理员的工作.在这个文件中,每个文件系统用一行来描述,在每一行中, ...

  4. Codeforces 768B - Code For 1(分治思想)

    768B - Code For 1 思路:类似于线段树的区间查询. 代码: #include<bits/stdc++.h> using namespace std; #define ll ...

  5. box-shadow四周都有阴影

    <style> .shadow{ -webkit-box-shadow: #666 0px 0px 10px; -moz-box-shadow: #666 0px 0px 10px; bo ...

  6. Java 常用对象-StringBuffer类

    2017-11-02 20:57:02 StringBuffer:线程安全的可变字符序列.一个类似于 String 的字符串缓冲区,但不能修改.虽然在任意时间点上它都包含某种特定的字符序列,但通过某些 ...

  7. ZOJ 2770 差分约束+SPFA

    Burn the Linked Camp Time Limit: 2 Seconds      Memory Limit: 65536 KB It is well known that, in the ...

  8. maven 3.5.2 修改java_home

        修改mvn.cmd文件,找到: @REM ==== START VALIDATION ==== if not "%JAVA_HOME%" == "" g ...

  9. 用POI导出excel时,较长的数字不想被自动变为科学计数法的解决方式(转)

    做过很多次导出excel了.都碰到一个问题,内容里如果包含一个比较长的数字,比如订单号“2546541656596”,excel会自动变成科学计数法... 弄过好几次都没有解决,最近又要导出excel ...

  10. EBS存储附件信息

    附件三种形式 1.文件 2.url 3.文本 三种方式存储不一样 1.文件是存blob 2.url是存一个链接信息,读出来的时候,就是一个蓝色可点链接    fnd_attached_document ...