class Solution {
public:
int binaryGap(int N) {
int position = ;
vector<int> V;
while (N)
{
if (N & )//N&1==1,表示最后一位是1
{
V.push_back(position);//把二进制为1的下标都记录下来
} position++;
N >>= ;//右移一位
} if (V.size() <= )
{
return ;
}
//1 2 4
int maxdistance = ;
int lastPosition = -;
for (int i = ; i < V.size(); i++)
{
cout << V[i] << " ";
if (i == )
{
lastPosition = V[i];
}
else
{
int distance = V[i] - lastPosition;
if (maxdistance < distance)
{
maxdistance = distance;
}
lastPosition = V[i];
}
}
return maxdistance;
}
};

leetcode868的更多相关文章

  1. [Swift]LeetCode868. 二进制间距 | Binary Gap

    Given a positive integer N, find and return the longest distance between two consecutive 1's in the ...

  2. Leetcode868.Binary Gap二进制间距

    给定一个正整数 N,找到并返回 N 的二进制表示中两个连续的 1 之间的最长距离. 如果没有两个连续的 1,返回 0 . 示例 1: 输入:22 输出:2 解释: 22 的二进制是 0b10110 . ...

随机推荐

  1. 【Hive】数据类型

    1.基本类型 整型:tinyint / samllint / int / bigint 浮点型:float / double / Decimals 布尔型:boolean 字符串:string / v ...

  2. SQL Server中的联合主键、聚集索引、非聚集索引

    我们都知道在一个表中当需要2列以上才能确定记录的唯一性的时候,就需要用到联合主键,当建立联合主键以后,在查询数据的时候性能就会有很大的提升,不过并不是对联合主键的任何列单独查询的时候性能都会提升,但我 ...

  3. Dockerfile 模版

    最近一直在用docker,总结了一个靠谱的模版,分享给大家. From ubuntu:14.04 MAINTAINER pidong.li@genetronhealth.com RUN echo de ...

  4. pip国内镜像(清华大学镜像)

    网上搜到的pip国内镜像大部分是豆瓣的 http://pypi.douban.com/simple/ 但是根本不全,很多包没有 所以推荐清华大学的 https://pypi.tuna.tsinghua ...

  5. RIPng 知识要点

    RIPng  --------------------------------------------------------- UDP:521 多播地址:FF02::9 -------------- ...

  6. c++使用http协议上传文件到七牛云服务器

    使用c++ http协议上传文件到七牛服务器时,比较搞的一点就是header的设置: "Content-Type:multipart/form-data;boundary=xxx" ...

  7. HDU - 6197:array array array (简单LIS)

    One day, Kaitou Kiddo had stolen a priceless diamond ring. But detective Conan blocked Kiddo's path ...

  8. Python之os.path

    os.path模块主要用于常用路径名操作,在编程中使用频率特高 os.path常用函数 abspath(path) 返回路径名path的规范化的绝对路径 split(path) 将path分割成目录和 ...

  9. 《DSP using MATLAB》示例Example7.23

    代码: wp = 0.2*pi; ws = 0.3*pi; Rp = 0.25; As = 50; [delta1, delta2] = db2delta(Rp, As); [N, f, m, wei ...

  10. 剑指offer-第5章优化时间和空间效率(丑数)

    题目:我们把只包含因子2,3,5的数叫做丑数.寻找第1500个丑数.通常把1当成第一个丑数. 思路1:第一步判断是否为丑数:丑数是只包含2,3,5的数,因此一定可以被2,3,5整除.通过求余数是否为零 ...