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 ...
随机推荐
- C#中怎么判断一个数组中是否存在某个数组值
(1) 第一种方法: ,,}; ); // 这里的1就是你要查找的值 ) // 不存在 else // 存在 (2) 第二种方法: string[] strArr = {"a",& ...
- C++ 智能指针四
/* 智能指针enable_shared_from_this模板类使用 */ #include <iostream> #include <string> #include &l ...
- MQTT 学习记录
学习mqtt协议,从网上找demo验证一下. 参考链接 https://www.jianshu.com/p/ebbe25d1c4b2 https://blog.csdn.net/xxmonstor/a ...
- Oracle调整内存超出限制出现ORA-27100: shared memory realm already exists问题解决办法
今天测试服务器遇到问题 ORA-04030:out of process memory when trying to allocate string bytes 一看就猜到是内存不足了,把Oracle ...
- 3 saltstack高可用
高可用 如果我们依赖于saltmaster管理服务器,那么就需要对saltmaster做好高可用.那么saltstack-master怎么做高可用呢? 可以参考官网1,官网2 机器配置: 我们有两台机 ...
- phpstrom2018
http://www.oyksoft.com/soft/40722.html?pc=1
- [OpenCV] Samples 18: Load image and check its attributes
本篇内容: * 图片读取 * 图片高宽 * 图片ROI * 图片缩放 Ref: http://blog.csdn.net/u012005313/article/details/51943442 官网2 ...
- JavaScript隐藏的坑一,隐式调用toString
最近在重新学习JavaScript,看动态原型对象的时候,打印了两个用同一个构造函数生成的对象,但是打印结果却不一样,请看代码: var box1=new Box(); console.log(box ...
- 131、ThreadLocal (转载)
http://blog.csdn.net/lufeng20/article/details/24314381 http://baike.baidu.com/link?url=7eL0qQm_5ULls ...
- Error running app: Default Activity Not Found
最近在调试安装Android Widget程序时,碰到Error running app: Default ActivityNot Found. 因为简单的Widget程序,如果不和应用程序关联,就不 ...