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. 解决MVC 时间序列化的方法

    1.全局处理 处理代码 publict static void SetSerializationJsonFormat(HttpConfiguration config) { // Web API co ...

  2. [原][OSG]整理osg渲染一帧的流程

    参考:最长的一帧 先看下frame void ViewerBase::frame(double simulationTime) { advance(simulationTime);//记录仿真时间,帧 ...

  3. Android 利用ViewPager、Fragment、PagerTabStrip实现多页面滑动效果

    本文主要介绍如何利用ViewPager.Fragment.PagerTabStrip实现多页面滑动效果.即google play首页.新浪微博消息(at.评论.私信.广播)页面的效果.ViewPage ...

  4. Skills CodeForces - 614D (贪心)

    链接 大意: $n$门课, 第$i$门分数$a_i$, 可以增加共$m$分, 求$cnt_{mx}*cf+mi*cm$的最大值 $cnt_{mx}$为满分的科目数, $mi$为最低分, $cf$, $ ...

  5. 删除php数组中的元素

    删除一个元素,且保持原有索引不变 使用 unset 函数,示例如下: <?php $array = array(0 => "a", 1 => "b&qu ...

  6. km板子(二分图最大权匹配)

    //#pragma comment(linker, "/stack:200000000") //#pragma GCC optimize("Ofast,no-stack- ...

  7. HDU 2157 矩阵幂orDP

    How many ways?? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  8. Java中List的排序方法

    方法一:实现Comparable接口 package com.java.test; public class Person implements Comparable<Person> { ...

  9. dubbo 心跳

    HeartBeatTask 类封装了心跳定时任务,需要了解的是 provider 和 consumer 都有可能发送心跳. final class HeartBeatTask implements R ...

  10. 手机端的META你知道多少

    一.天猫 <title>天猫触屏版</title> <meta content="text/html; charset=utf-8" http-equ ...