题目要求

Given a positive integer N, find and return the longest distance between two consecutive 1's in the binary representation of N.

If there aren't two consecutive 1's, return 0.

题目分析及思路

给定一个正整数,返回该数二进制形式中连续两个1的最长间隔。若是没有连续的两个1,则返回0。可将该数转成二进制形式,得到的结果是个字符串,切片得到二进制形式。再以1进行分割。去掉结果列表的首尾元素,对剩余列表元素求长度并加1得到最后的间隔列表。若列表为空,则返回0;否则返回列表中的最大值。

python代码

class Solution:

def binaryGap(self, N: int) -> int:

dis = []

b = bin(N)

b = b[2:]

gaps_str = b.split('1')

gaps_str.pop()

gaps_str.reverse()

gaps_str.pop()

gaps_int = [len(g)+1 for g in gaps_str]

if not gaps_int:

return 0

else:

return max(gaps_int)

LeetCode 868 Binary Gap 解题报告的更多相关文章

  1. 【LeetCode】868. Binary Gap 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 线性扫描 日期 题目地址:https://leetc ...

  2. LeetCode: Balanced Binary Tree 解题报告

    Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, a he ...

  3. LeetCode - 868. Binary Gap

    Given a positive integer N, find and return the longest distance between two consecutive 1's in the ...

  4. 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)

    [LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

  5. 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)

    [LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...

  6. 【LeetCode】331. Verify Preorder Serialization of a Binary Tree 解题报告(Python)

    [LeetCode]331. Verify Preorder Serialization of a Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...

  7. 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)

    [LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...

  8. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  9. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

随机推荐

  1. PSR PHP业界规范

    0x0 大型项目的问题 随着项目越来越大,参与的人数越来越多,代码变得越来越不可维护了. 每个人都给项目带来自己的风格,所以这时就需要大家采用一个统一的标准. 0x1 解决办法 于是顶尖的PHPer们 ...

  2. PentesterLab渗透演练平台

    转载自: https://www.blackh4t.org/archives/1143.html http://www.91ri.org/5958.html     1.  什么是WebApp Pen ...

  3. 【iCore1S 双核心板_FPGA】例程十六:基于SPI的ARM与FPGA通信实验

    实验现象: 核心代码: int main(void) { int i,n; ]; ]; HAL_Init(); system_clock.initialize(); led.initialize(); ...

  4. Repeater数据控件的两个重要事件ItemDataBound 和 ItemCommand

    1 ItemDataBound:数据绑定的时候(正在进行时)发生. 2 ItemCommand :用来响应Item模板中的控件的事件. 如下代码 aspx代码: [html] view plain c ...

  5. Vue中的computed属性

    阅读Vue官网的过程中,对于计算属于与监听器章节的内容有点理解的不清晰:https://cn.vuejs.org/v2/guide/computed.html. 后来上网查询了资料,结合官网的说明,总 ...

  6. 在android 上 使用 rxjava 入门篇

    什么是 rxJava? RxJava is a Java VM implementation of Reactive Extensions: a library for composing async ...

  7. greendao引起的NoClassDefFoundError异常解决

    在使用Android studio导入eclipse工程师报错,因为原工程引用了greendao的第三方工程包 java.lang.NoClassDefFoundError: org.greenrob ...

  8. (笔记)Linux内核学习(二)之进程

    一 进程与线程 进程就是处于执行期的程序,包含了独立地址空间,多个执行线程等资源. 线程是进程中活动的对象,每个线程都拥有独立的程序计数器.进程栈和一组进程寄存器. 内核调度的对象是线程而不是进程.对 ...

  9. Java如何使套接字向单个客户端显示消息?

    在Java编程中,如何使用套接字向单个客户端显示消息? 以下示例演示了如何使用Socket类的ssock.accept()方法向单个套接字客户端上显示消息. package com.yiibai; i ...

  10. react-router 4.3 js实现跳转

    import React, {Component} from 'react'; import { NavLink,Link } from "react-router-dom"; i ...