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.

 

Example 1:

Input: 22
Output: 2
Explanation:
22 in binary is 0b10110.
In the binary representation of 22, there are three ones, and two consecutive pairs of 1's.
The first consecutive pair of 1's have distance 2.
The second consecutive pair of 1's have distance 1.
The answer is the largest of these two distances, which is 2.

Example 2:

Input: 5
Output: 2
Explanation:
5 in binary is 0b101.

Example 3:

Input: 6
Output: 1
Explanation:
6 in binary is 0b110.

Example 4:

Input: 8
Output: 0
Explanation:
8 in binary is 0b1000.
There aren't any consecutive pairs of 1's in the binary representation of 8, so we return 0.

Code

class Solution(object):
def binaryGap(self, N):
s, pre, ans = bin(N)[2:], 0, 0
for index, c in enumerate(s):
if c == '':
ans = max(ans, index - pre)
pre = index
return ans

[LeetCode] 868. Binary Gap_Easy的更多相关文章

  1. LeetCode - 868. Binary Gap

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

  2. LeetCode 868 Binary Gap 解题报告

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

  3. leetcode 199 :Binary Tree Right Side View

    // 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...

  4. LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal

    LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...

  5. LeetCode:Unique Binary Search Trees I II

    LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...

  6. LeetCode: Validata Binary Search Tree

    LeetCode: Validata Binary Search Tree Given a binary tree, determine if it is a valid binary search ...

  7. (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal

    Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...

  8. leetcode 199. Binary Tree Right Side View 、leetcode 116. Populating Next Right Pointers in Each Node 、117. Populating Next Right Pointers in Each Node II

    leetcode 199. Binary Tree Right Side View 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...

  9. [LeetCode] 549. Binary Tree Longest Consecutive Sequence II_ Medium tag: DFS recursive

    Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...

随机推荐

  1. nodejs抓取页面内容,并分析有无某些内容的js文件

    nodejs获取网页内容绑定data事件,获取到的数据会分几次相应,如果想全局内容匹配,需要等待请求结束,在end结束事件里把累积起来的全局数据进行操作! 举个例子,比如要在页面中找有没有www.ba ...

  2. iis和tomcat(整合)

    ---恢复内容开始---   (一)   为什么要把IIS.Tomcat整合到一起?假如你遇到这种情况,你开发了一个javaweb项目要部署到服务器上,但是这个服务器上已经部署了asp.asp.net ...

  3. li下的ul----多级列表

    <ul id="ul_Style1"> <li>第1级第1行</li> <li> <ul id="ul_Style2 ...

  4. CentOS7 vsftpd 安装及配置

    0x:卸载vsftpd [root@localhost ~]# yum remove vsftpd [root@localhost ~]# find / -name "vsftpd*&quo ...

  5. atoi函数的用法

    库函数原型: #inclue <stdlib.h> int atoi(const char *nptr); 用法:将字符串里的数字字符转化为整形数.返回整形值. 注意:转化时跳过前面的空格 ...

  6. 微信、QQ群短文本聊天语料总结

    在文本分类任务中,语料的特性千差万别,我们需要找到适合模型并抓住数据的特性,最终才能得到较好的model.最近在文本类别标注任务,就是给文本打标签确定该文本的类别.这是一个很费人工的过程,需要认真仔细 ...

  7. python--json串相关的loads dumps load dump

    #1 json串长的像字典,但不是字典类型,是str类型 #例如:user_info为json串,dict为字典,如果txt文本中标识dict的内容 为json串user_info = '''{&qu ...

  8. day0320 时间模块 collection模块

    一. TIME模块 python提供了一个time和calendar模块可以用于格式化日期和时间. 时间间隔一秒为单位. 每个时间戳都以1970年1月1日午夜经过多长时间来表示. 1.时间戳 函数ti ...

  9. AVD启动报错:Running an x86 based Android Virtual Device (AVD) is 10x faster

    1.cmd窗口中输入emulator -avd test 启动AVD时报错: Running an x86 based Android Virtual Device (AVD) is 10x fast ...

  10. quartz 2.1.6使用方法

    /** * @Description: * * @Title: QuartzManager.java * @Package com.joyce.quartz * @Copyright: Copyrig ...