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 ...
随机推荐
- MXNET:多层感知机
从零开始 前面了解了多层感知机的原理,我们来实现一个多层感知机. # -*- coding: utf-8 -*- from mxnet import init from mxnet import nd ...
- 【WPF】点击滑动条(Slider),移动滑块(Tick)到鼠标点击的位置
问题:点击Slider控件时,滑块会自动跳到滑动条的最边缘位置,无法跳到鼠标点击的位置上. 办法:给Slider控件设置属性IsMoveToPointEnabled="True"即 ...
- 使用ActivityGroup需要注意的地方
Group里面的所有“孩子Activity”不能直接调用finish(),只能等ActivityGroup调用它自己的finish()后,会自动调用所有“孩子Activity”的finish().
- R语言reads.table 自动将字符串变成了逻辑值
今天遇到了一个问题,文件中有一列的值为全为F, 用read.table 读取的时候,自动将F 变成了false 对于这样的转换,可以通过 colClass 参数控制 colClass 参数指定每一列的 ...
- c# 正则匹配对称括号
https://stackoverflow.com/questions/7898310/using-regex-to-balance-match-parenthesis
- 在linux中安装字体
https://blog.csdn.net/wangxintong_1992/article/details/81194896
- [Bayes] Metroplis Algorithm --> Gibbs Sampling
重要的是Gibbs的思想. 全概率分布,可以唯一地确定一个联合分布 ---- Hammersley-Clifford 多元高斯分布 当然,这个有点复杂,考虑个简单的,二元高斯,那么超参数就是: 二元高 ...
- Web上传文件的原理及实现
现在有很多Web程序都有上传功能,实现上传功能的组件或框架也很多,如基于java的Commons FileUpload.还有Struts1.x和Struts2中带的上传文件功能(实际上,Struts2 ...
- trace/trace2命令
send REPLICAT REP_1B trace2 /home/oracle/trace2.log send REPLICAT REP_1B trace /home/oracle/trace.lo ...
- String对象的比较
public class StringTest { /* * equals 和 ==的区别 * 如果类中没有重写equals(),那么默认比较也是内存地址 * ==在基本数据类型中比较的是值! * i ...