[LeetCode&Python] Problem 868. Binary Gap
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.
Note:
1 <= N <= 10^9
class Solution:
def binaryGap(self, N):
"""
:type N: int
:rtype: int
"""
bN=bin(N)[2:] n=len(bN) ans=0
preOne=-1 for i in range(n):
if bN[i]=='1':
if preOne==-1:
preOne=i
else:
if ans<=(i-preOne):
ans=(i-preOne)
preOne=i return ans
[LeetCode&Python] Problem 868. Binary Gap的更多相关文章
- [LeetCode&Python] Problem 257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...
- [LeetCode&Python] Problem 107. Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- [LeetCode&Python] Problem 401. Binary Watch
A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom ...
- [LeetCode&Python] Problem 563. Binary Tree Tilt
Given a binary tree, return the tilt of the whole tree. The tilt of a tree node is defined as the ab ...
- [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 ...
- [LeetCode&Python] Problem 704. Binary Search
Given a sorted (in ascending order) integer array nums of n elements and a target value, write a fun ...
- 【Leetcode_easy】868. Binary Gap
problem 868. Binary Gap solution1: class Solution { public: int binaryGap(int N) { ; vector<int&g ...
- 【LeetCode】868. Binary Gap 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 线性扫描 日期 题目地址:https://leetc ...
- LeetCode 868 Binary Gap 解题报告
题目要求 Given a positive integer N, find and return the longest distance between two consecutive 1's in ...
随机推荐
- [设计模式][c++]状态切换模式
转自:http://blog.csdn.net/yongh701/article/details/49154439 状态模式也是设计模式的一种,这种设计模式思想不复杂,就是实现起来的代码有点复杂.主要 ...
- TTFB
TTFB Time To First Byte 1.含义 最初的网络请求被发起”到“从服务器接收到第一个字节前”所花费的毫秒数 包含了 TCP连接时间,发送HTTP请求时间和获得响应消息第一个字 ...
- Android JNI学习(四)——JNI的常用方法的中文API
本系列文章如下: Android JNI(一)——NDK与JNI基础 Android JNI学习(二)——实战JNI之“hello world” Android JNI学习(三)——Java与Nati ...
- Android JNI学习(三)——Java与Native相互调用
本系列文章如下: Android JNI(一)——NDK与JNI基础 Android JNI学习(二)——实战JNI之“hello world” Android JNI学习(三)——Java与Nati ...
- JavaScript运算符与类型
1.运算符优先级 首先讲一下运算符的优先级,它决定了表达式中运算执行的先后顺序,优先级高的运算符最先被执行. 下面的表将所有运算符按照优先级的不同从高到低排列: 优先级 运算类型 关联性 运算符 19 ...
- 在WPF中添加Windows Form控件(包括 ocx控件)
首先,需要向项目中的reference添加两个dll,一个是.NET库中的System.Windows.Forms,另外一个是WindowsFormsIntegration,它的位置一般是在C:\ ...
- Win7 默认.lnk打开方式全是别的程序 还原的办法
Xu言: no zuo no die~ 今天,一个朋友问我,他电脑桌面上点任何东西都是提示下载... - -||| 本以为是中毒了,然后上去看了一眼..发现他自己把所有.lnk 的默认打开方式选择了搜 ...
- (转)理解TIME_WAIT,彻底弄清解决TCP: time wait bucket table overflow
转载自http://blog.51cto.com/benpaozhe/1767612: 一直对这个问题知其然而不知其所以然,这些日子再次碰到,看了很多的资料,彻底解决一下,呵呵,先上个图,所有理解围绕 ...
- java.lang.UnsupportedClassVersionError: com/my/test/TestUser : Unsupported major.minor version 52.0
问题原因: 1.执行代码的jdk版本 低于 编译的jdk版本 2.项目用JDK1.8运行过,现在又在本地的eclipse等开发工具或者本地环境变量为低版本的jdk1.7或者jdk1.6下运行,ecli ...
- robot framework学习笔记2
声明:本笔记都只是自己根据大牛虫师的robot系列文档学习记录的,学习的话还请移步虫师博客:https://www.cnblogs.com/fnng/ 非常感谢大牛的分享,带小白一步一步入门 F5 ...