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.

判断"00" 和"11"是否在str(bin(n))里面即可.

Code

class Solution:
def alternateBits(self, n):
n = bin(n)
return "" not in n and "" not in n

[LeetCode] 693. Binary Number with Alternating Bits_Easy的更多相关文章

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

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

  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. 【LeetCode】693. Binary Number with Alternating Bits 解题报告(Python)

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

  5. [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 ...

  6. 693. Binary Number with Alternating Bits

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

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

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

  8. LeetCode算法题-Binary Number with Alternating Bits(Java实现)

    这是悦乐书的第292次更新,第310篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第160题(顺位题号是693).给定正整数,检查它是否具有交替位:即它的二进制数的任意两 ...

  9. LeetCode题解之Binary Number with Alternating Bits

    1.题目描述 2.问题分析 将数值转换为二进制,然后将前面的 0 去掉,再遍历一边二进制字符串,对每个字符和其后部的字符进行比较. 3.代码 bool hasAlternatingBits(int n ...

随机推荐

  1. Windows 10 Install rabbitmq-server-3.6.9

    rabbitmq下载 http://www.rabbitmq.com/releases/rabbitmq-server/v3.6.9/rabbitmq-server-3.6.9.exe otp插件下载 ...

  2. 在eclipse中导入hadoop jar包,和必要时导入源码包。

    1. 解药hadoop包 1, C:\hadoop-2.7.2\share\hadoop  提取出所有的 jar 包, 到 _lib 文件夹下 2,将有含有source 名称的jar包 剪切出来 3, ...

  3. [No0000ED]IPSec策略之管理

    IP安全策略 @echo off :again set num= set ippolicyname= set ismmpfs= set keytime= set keyexpress= set new ...

  4. hive优化之调整mapreduce数目

    一.调整hive作业中的map数 1.通常情况下,作业会通过input的目录产生一个或者多个map任务.主要的决定因素有: input的文件总个数,input的文件大小,集群设置的文件块大小(目前为1 ...

  5. iOS FMDB的是使用和注意事项

    1.FMDB 默认的使用方法不是线程安全的. 2.Sqlite 默认不支持外键. 3.Sqlite 不支持用 ALTER 关键字给已有表添加外键约束 解决: 1.FMDBDatabaseQueue 2 ...

  6. VS在解决方案中添加一个别人给的项目,我自己的项目主窗体中不能调用

    提示缺少Using引用,我在主窗体中已经写了Using XX,还是提示“未能找到类型或命名空间名“ XX”(是否缺少Using指令或程序集引用?)”,以前只要Using 一下就好了,后来想了一下,要在 ...

  7. OPTIMIZE TABLE linked list 表优化原理 链表数据结构 空间再利用

    小结: 1.加快读写: 2.对于InnoDB表,在一定条件下,通过复制旧表重建: 3.实践中, 3.1.show processlist;查看线程,发现,认为堵塞读请求: 3.2.数据长度空间不变,索 ...

  8. MachineLearning:

    https://github.com/pennyliang/MachineLearning-C---code https://zhuanlan.zhihu.com/p/22794772 http:// ...

  9. rman checksyntax和解决RMAN-01009: syntax error: found "dot"

    在日常清归档时候执行脚本报错 RMAN-00552: syntax error in command line argumentsRMAN-01009: syntax error: found &qu ...

  10. 使用@import导入实现了ImportBeanDefinitionRegistrar接口的类,不能被注册为bean

    今天在调试公司spring项目的时候发现了这样一个问题,由于我们的项目使用的是springboot就以springboot为例,代码如下: @Import({DataSourceRegister.cl ...