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. VcCallC#_02

    1.VC代码:(vs2013运行正常) // ConsoleApplication_CallCS.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h&quo ...

  2. HDU 6124 Euler theorem

    Euler theorem 思路:找规律 a       余数                  个数 1       0 1                     2 2       0 2   ...

  3. [转]mysql-mmm集群(多实例)

    一.需求说明 最近一直在学习mysql-mmm,想以后这个架构也能用在我们公司的业务上,我们公司的业务是单机多实例部署,所以也想把mysql-mmm部署成这样,功夫不负有心人,我成功了,和大家分享一下 ...

  4. [Java学习] Java Object类

    Object 类位于 java.lang 包中,是所有 Java 类的祖先,Java 中的每个类都由它扩展而来. 定义Java类时如果没有显示的指明父类,那么就默认继承了 Object 类.例如: 1 ...

  5. Up and Down the Tree CodeForces - 1065F (树形dp)

    链接 题目大意:给定$n$结点树, 假设当前在结点$v$, 有两种操作 $(1)$移动到$v$的子树内任意一个叶子上 $(2)$若$v$为叶子, 可以移动到距离$v$不超过$k$的祖先上 初始在结点$ ...

  6. python 使用yield进行数据的流式处理

    demo:从文件中取包含字符“a”的5行数据做一次批处理!!! # coding: utf-8 import time def cat(f): for line in f: yield line de ...

  7. ShiroFilterFactoryBean 处理拦截资源文件问题(Shiro权限管理)

    一.需要定义ShiroFilterFactoryBean()方法,而ShiroFilterFactoryBean.class是实现了FactoryBean和BeanPostProcessor接口: 1 ...

  8. C++:线程(std::thread)

    1.创建一个线程 创建线程比较简单,使用std的thread实例化一个线程对象就创建完成了,示例: #include <iostream> #include <thread> ...

  9. sha256 in C language

    sha256.h #ifndef _SHA256_H#define _SHA256_H #ifndef uint8#define uint8 unsigned char#endif #ifndef u ...

  10. 第n+1次考试

    题目: 1. 中位数 [问题描述] 给定C个不同物品,每个物品有一重量和体积,保证每个物品的重量不一样.从中选出N个物品,在体积不超过F的情况下,使得选出的物品的重量的中位数最大.所谓中位数,就是排序 ...