[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 ...
随机推荐
- python2 安装scrapy出现错误提示解决办法~
首先:set STATICBUILD=true && pip install lxml 安装环境: windows7操作系统,已经正确安装python,pip. 使用pip功能安装Sc ...
- [原][OSG]整理osg渲染一帧的流程
参考:最长的一帧 先看下frame void ViewerBase::frame(double simulationTime) { advance(simulationTime);//记录仿真时间,帧 ...
- Servlet之javax.servlet包
链接 : http://blog.sina.com.cn/s/blog_5d4214c70102wnf1.html
- Codeforces D - Ithea Plays With Chtholly
D - Ithea Plays With Chtholly 思路:考虑每个位置最多被替换c/2次 那么折半考虑,如果小于c/2,从左往右替换,大于c/2总右往左替换,只有小于这个数(从左往右)或者大于 ...
- 顶点与UV
1.顶点坐标和UV坐标是三维模型重要的两个坐标系统. 2.什么是UV?UV分别是图像在显示器水平和垂直方向上坐标,值在 0 - 1 之间 ,即水平方向的第 U 个做像素/图片宽度,垂直方向的第 V 个 ...
- [.NET开发] C#使用doggleReport生成pdf报表的方法
本文实例讲述了C#使用doggleReport生成pdf报表的方法.分享给大家供大家参考,具体如下: 1. 安装nuget -install package DoddleReport -install ...
- scRNA-seq单细胞测序数据分析工具汇总
本文总结自一篇综述: Computational approaches for interpreting scRNA-seq data 单细胞分析分为两个层次: cell level gene lev ...
- Spring Cloud 学习网址
1. https://blog.csdn.net/forezp/article/details/70148833 史上最简单的 SpringCloud 教程 (非常适合新手快速上手教程)2.http ...
- Confluence 6 嵌套用户组的示例
示例 1 : 用是一个子用户组成员 想象在你的目录服务器中,存在下面 2 个用户组: staff marketing 成员: marketing 用户组是 staff 的成员. 用户 jsmith ...
- ubuntu mysql主从库的搭建
1,首先我们要确定一个从库一个主库,紧记从库只能读取不能有其他的操作,如果操作写那主从就失效了,那就看看我们这么搭建主从吧! 2. 环境:Ubuntu,Mysql (主从的数据库版本必须保持一致) 主 ...