LeetCode 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.
题目分析及思路
给定一个正整数,返回该数二进制形式中连续两个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 解题报告的更多相关文章
- 【LeetCode】868. Binary Gap 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 线性扫描 日期 题目地址:https://leetc ...
- LeetCode: Balanced Binary Tree 解题报告
Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, a he ...
- LeetCode - 868. Binary Gap
Given a positive integer N, find and return the longest distance between two consecutive 1's in the ...
- 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)
[LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...
- 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)
[LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...
- 【LeetCode】331. Verify Preorder Serialization of a Binary Tree 解题报告(Python)
[LeetCode]331. Verify Preorder Serialization of a Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...
- 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)
[LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
随机推荐
- TCP/IP协议理解
TCP/IP协议理解 一. 协议分层 由于ISO标准中的网络模型是个7层模型,但是由于7层模型对于当时的网络厂商来说太过复杂,很多厂家采用了简化的4层模型来实现网络设备,4层模型也就成了事实的网 ...
- nginx配置http为1.0到1.1
转载自:https://blog.csdn.net/u014558668/article/details/79237020 需求:接口通过nginx转发服务器,接收不到数据,但是测试环境是好的: 环境 ...
- 【iCore1S 双核心板_FPGA】例程十六:基于SPI的ARM与FPGA通信实验
实验现象: 核心代码: int main(void) { int i,n; ]; ]; HAL_Init(); system_clock.initialize(); led.initialize(); ...
- android 监听动画对象后不能播放动画
采用监听 AnimationListener 发现不能播放动画了. 解决办法: 将动画的启动方式:animation.startnow去掉,改为如下即可 view.startAnimation(an ...
- git排除常用配置,svn与git共存时.gitignore配置
#idea与myeclipse配置文件全部排除 *.class #package file*.war*.ear #kdiff3 ignore*.orig #maven ignoretarget/ #e ...
- 数据中心内负载均衡-ECMP的使用分析
背景 数据中心的网络拓扑通常采用CLOS结构,主机之间常存在多条路径.数据中心为满足吞吐量敏感型流量的需求会提供大量的带宽资源.那么利用数据中心这种网络拓扑已知,路径资源.带宽资源丰富的特性,可以更好 ...
- Mysql系列二:Mysql 开发标准规范
原文链接:http://www.cnblogs.com/liulei-LL/p/7729983.html 一.表设计 1. 库名.表名.字段名使用小写字母,“_”分割. 2. 库名.表名.字段名不超过 ...
- 嵌入式开发之hi3519---PCIE DMA
http://blog.csdn.net/abcamus/article/details/76167747 大话pcie dma http://blog.csdn.net/qingfengtsing/ ...
- Java面试题考点全面总结
我通过两个月的复习拿到了阿里巴巴的 offer,有一些运气,也有一些心得,借着跳槽季来临特此分享出来. 简单梳理一下我的复习思路,同时也希望和大家一起交流讨论,一起学习,如果不对之处欢迎指正一起学习. ...
- No suitable servers found (`serverselectiontryonce` set): [Failed connecting to '115.28.161.44:27017': Connection timed out] php mongodb 异常
我 php mongodb 拓展使用的是 MongoDB driver 今天查询数据的时候 偶尔会提示, No suitable servers found (`serverselectiontry ...