题目要求

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

题目分析及思路

给定一个正整数,判断它的二进制形式是否是01交替出现。如果是则返回True,否则返回False。可以先获得该数的二进制形式,之后利用切片分别获得其奇数位和偶数位的值。若全为1或0,则是满足要求的数。

python代码

class Solution:

def hasAlternatingBits(self, n: int) -> bool:

b_str = bin(n)

b_substr = b_str[2:]

evens = b_substr[0::2]

odds = b_substr[1::2]

if evens.count('1') == len(evens) and odds.count('0') == len(odds):

return True

else:

return False

LeetCode 693 Binary Number with Alternating Bits 解题报告的更多相关文章

  1. 【LeetCode】693. Binary Number with Alternating Bits 解题报告(Python)

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

  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&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 ...

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

  6. 【LeetCode】191. Number of 1 Bits 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 右移32次 计算末尾的1的个数 转成二进制统计1的个 ...

  7. 693. Binary Number with Alternating Bits

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

  8. 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)

    [LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

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

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

随机推荐

  1. 不8000就业,不还实习费的AICODER全栈实习二期开始报名

    4月17日是个伟大的日子,AICODER全栈实习一期班,正式开始!伙伴们已经撸起袖子加油干了. 二期班开始报名 二期班定于5月17日开班,从二期班开始,实习费用调整如下: 三个月模式实习费,调整为12 ...

  2. Node入门教程(9)第七章:NodeJs的文件处理

    Node的文件处理涉及到前面说的ptah模块,以及fs文件系统.stream流处理.Buffer缓冲器等模块.内容可能比较多,相关内容请以官网文档为主,此处主要以案例讲解为主,分享给大家一些常用的经典 ...

  3. 【iCore1S 双核心板_FPGA】例程十三:FSMC总线通信实验——复用地址模式

    实验原理: STM32F103上自带FMC控制器,本实验将通过FMC总线的地址复用模式实现STM32与FPGA 之间通信,FPGA内部建立RAM块,FPGA桥接STM32和RAM块,本实验通过FSMC ...

  4. 安装Node和NPM

      1.node和NPM是什么?   Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js ...

  5. 【转】详解在visual studio中使用git版本系统(图文)

    http://blog.csdn.net/wojilu/article/details/6976230 很多人已经在使用git(或正在转移到git上),在github.com上,也看到不少国内同学的开 ...

  6. [UFLDL] *Train and Optimize

    Deep learning:三十七(Deep learning中的优化方法) Deep learning:四十一(Dropout简单理解) Deep learning:四十三(用Hessian Fre ...

  7. Eclipse设置默认的换行长度

    1. 点击Window->Preferences->Java->Code Style->Formatter 2. 点击New,给profile随意取个名字,点击OK 3. Ma ...

  8. Html 助手

    /*! * Html 助手 * version: 1.0.0-2018.07.25 * Requires ES6 * Copyright (c) 2018 Tiac * http://www.cnbl ...

  9. Using Information Fragments to Answer the Questions Developers Ask

    content : 1.采访了11个开发者,获得78个常问的问题:2.对78个问题进行分类,分为8类:These questions span eight domains of information ...

  10. WPS 2019 多个sheet表拆分成独立的excel文件

    参考: https://www.cnblogs.com/hackxiyu/p/8945975.html 场景:将多个sheet表拆分成独立的excel文件 一.安装VB工具: 默认情况下:wps -- ...