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. Android开发实战(申明:来源于网络)

    Android开发实战(申明:来源于网络) 地址:http://so.csdn.net/so/search/s.do?q=Android%E5%BC%80%E5%8F%91%E5%AE%9E%E6%8 ...

  2. 记一次treegrid checkbox 选择问题

    最后选择 select 方法来实现 checkRow checked 属性也不行 getChecked 方法并不能取到这两种方法的行数据

  3. 从 Firefox 35 版本开始,就无法兼容 PAC 式代理

    经过反复的测试,包括在“高级”选项里启用 PAC 代理的设置,也都无法使用 PAC 的代理——无法登陆 Twitter 账号,无法打开 Google 网页. 不知道各位有什么好办法吗? 以及中文火狐社 ...

  4. Cnblog Markdown编辑器

    第一次使用Cnblog Markdown编辑器 1.在博客园使用 Markdown 与 LATEX[1] 1.1.开启MarkDown编辑器 选择 Markdown 撰写博文:在博客园管理-选项中,将 ...

  5. 关于Dosbox0.74无法使用masm命令

    今天尝试在dosbox里编译asm源代码文件 但是提示“illegal command”,也就是非法命令 开始还以为我的dosbox版本不对 但是去网上查阅资料发现别人用这个版本都可以使用 所以百思不 ...

  6. tensorflow模型在实际上线进行预测的时候,使用CPU工作

    最近已经训练好了一版基于DeepLearning的文本分类模型,TextCNN原理.在实际的预测中,如果默认模型会优先选择GPU那么每一次实例调用,都会加载GPU信息,这会造成很大的性能降低. 那么, ...

  7. 【libreOJ模板】并查集(输入挂,取模与find优化)

    1.了解了各种输入挂性orz,找到了一个合适的 2.find用while写能快一倍,并且能被数据卡掉 3.取模只能快十几毫秒,但也能被数据卡掉 取模find双优化是1997mm过的 再加一个性价比较高 ...

  8. 【瞎搞题】gym226123 L. For the Honest Election

    http://codeforces.com/gym/226123/problem/L 题解:手摸几组数据,发现一个规律:将n 质因数分解,答案就是 每个质因数/2+1后再乘起来. 然后考虑2的情况: ...

  9. hive优化之开启压缩功能

    1.开启hive作业mapreduce任务中间压缩功能: 对于数据进行压缩可以减少job中map和reduce task间的数据传输量.对于中间数据压缩,选择一个低cpu开销编/解码器要不选择一个压缩 ...

  10. python字符串常用方法

    #1.strip()去掉空格(字符串首.尾空格).lstrip()去掉左侧空格.rstrip()去掉右侧空格print(' abc '.lstrip())#>>abcprint(' abc ...