题目要求

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. MXNET:丢弃法

    除了前面介绍的权重衰减以外,深度学习模型常常使用丢弃法(dropout)来应对过拟合问题. 方法与原理 为了确保测试模型的确定性,丢弃法的使用只发生在训练模型时,并非测试模型时.当神经网络中的某一层使 ...

  2. 模仿ReentrantLock类自定义锁

    简介 临近过年了,没什么需求,今天模仿ReentrantLock自定义写了一个自己锁,在这里记录一下,前提是对AQS原理有所了解,分享给大家 1.自定义锁MyLock package com.jack ...

  3. iTunes空间不足无法备份iphone的问题

    因为换手机,需要把旧iphone备份出来,在新iphone上恢复.使用mac进行备份时总是提示iTunes无法备份,所用电脑空间不够,即使有个用空间的移动硬盘也无法备份.网上的方法都是说修改iTune ...

  4. OpenResty 最佳实践 1

    建议先搜索<OpenResty最佳实践.pdf> 到网上下载openresty-1.13.6.1-win32 考虑到操作方便性,建议建立个bin目录,放入系统目录中,生成 nginx-st ...

  5. C语言之单元测试

    在ITOO高校云平台项目实践中,我们模板的模块因为在调别人的接口时出现了问题,为了弄明白是不是接口出了问题,就必须学会单元测试. WHAT? 单元测试(unit testing),是指对软件中的最小可 ...

  6. Android中Sqlite数据库多线程并发问题

    最近在做一个Android项目, 为了改善用户体验,把原先必须让用户“等待”的过程改成在新线程中异步执行.但是这样做遇到了多个线程同时需要写Sqlite数据库,导致操作数据库失败. 本人对Java并不 ...

  7. ABBYY FineReader 12使用教程

    说到OCR图文识别软件,自然少不了ABBYY FineReader 12.ABBYY FineReader 12可快速方便地将扫描纸质文档.PDF文件和数码相机的图像转换成可编辑.可搜索的文本.我们已 ...

  8. c++ union内存

    看一个例子: #include <iostream> #include <stdio.h> using namespace std; union A { int a; stru ...

  9. Windows下MySQL的绿化与精简

    MySQL本身就支持安装使用,本文只是对自己使用免安装版MySQL的经历记录下来,以便以后查看. 首先是获取Windows下的MySQL免安装版本,这个需要去到MySQL官网进行下载.我一般喜欢把首页 ...

  10. HTTP协议中GET和POST方法的区别

    转载 通常的理解 w3schools关于这个问题的解答:HTTP 方法:GET 对比 POST 列出了一般的理解: 方法 GET POST 后退按钮/刷新 无害 数据会被重新提交(浏览器应该告知用户数 ...