[LeetCode] 868. Binary Gap_Easy
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的更多相关文章
- LeetCode - 868. Binary Gap
Given a positive integer N, find and return the longest distance between two consecutive 1's in the ...
- LeetCode 868 Binary Gap 解题报告
题目要求 Given a positive integer N, find and return the longest distance between two consecutive 1's in ...
- leetcode 199 :Binary Tree Right Side View
// 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...
- 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 ...
- LeetCode:Unique Binary Search Trees I II
LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...
- LeetCode: Validata Binary Search Tree
LeetCode: Validata Binary Search Tree Given a binary tree, determine if it is a valid binary search ...
- (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...
- 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 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...
- [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 ...
随机推荐
- yum install 下载后保存rpm包
keepcache=0 更改为1下载RPM包 不会自动删除 vi /etc/yum.conf [main] cachedir=/var/cache/yum/$basearch/$releasever ...
- windows10安装
https://www.microsoft.com/zh-cn/software-download/windows10
- AudioUnit录音和播放同时进行的一些注意点
录音(播放)和暂停 -(void)start { self.soundTotalLength = 0.0f; if (!self.unitHaveStart) { NSError *error = n ...
- linux状态及原理全剖析
Table of Contents 1 linux 1.1 proc filesystem 1.1.1 /proc 1.1.1.1 /proc/meminfo 1.1.1.2 /proc/stat 1 ...
- php值callback类型和匿名函数(闭包)
callback.callable类型 自PHP5.4起可以使用callable类型制定回调类型callback. 本文档基于同样理由使用callback类型信息. 一些函数如call_user_fu ...
- [administrator][driver] driverctl 是如何在udev上层管理设备驱动的
https://gitlab.com/driverctl/driverctl driverctl 处于 kernel 与 udev做设备与驱动管理的上层. 理解什么叫override是本文的核心内容. ...
- [qemu][kvm] 在一个vmware虚拟机里安装qemu-kvm虚拟机
说起来这个需求,简直是傻傻的.但却实实在在的摆在我的面前.... VM无外乎就是为了模拟场景:我现在要的场景就是一台很多个core的linux主机.但是我只有一个装了windows的笔记本.上边有一个 ...
- [development][dpdk] dpdk-pmdinfo 工具的使用
发现了一个工具 dpdk-pmdinfo 但是文档介绍的很不清楚, http://dpdk.org/doc/guides/tools/pmdinfo.html 经过摸索, 这可能就是一个查询 dpdk ...
- [knowledge] big data things
http://hadoop.apache.org/ https://spark.apache.org/ https://nifi.apache.org/ https://www.cloudera.co ...
- CSDN论坛 > Delphi > VCL组件开发及应用 DBLookupComboBox用法
(1)DataSource属性 该属性用于连接要编辑数据的主表数据源(2)DataField属性 该属性用于指定要编辑的数据字段名(3)ListSource属性 . 该属性用于 ...