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. 29-THREE.JS 根据公式画形状

    <!DOCTYPE html> <html> <head> <title></title> <script src="htt ...

  2. 广义线性模型(GLM)

    一.广义线性模型概念 在讨论广义线性模型之前,先回顾一下基本线性模型,也就是线性回归. 在线性回归模型中的假设中,有两点需要提出: (1)假设因变量服从高斯分布:$Y={{\theta }^{T}}x ...

  3. flash代码

    Flash常用的动作命令一.Flash中的常用命令:1.在当前帧停止播放 on(release){ stop();} 2.从当前帧开始播放 on(release){ play();} 3.跳到第 10 ...

  4. css3 伪类

    ::selection { 选中后的样式 } 链接 p:only-child     p的父级只有一个p标签 p:only-of-type   p的父级有一个p标签, 但还可以包含其他标签 p:fis ...

  5. scrapy 碎片

    1.启动命令 2.目录结构 3.文件说明 4.架构图示 5.代码流程 参考资料: http://www.cnblogs.com/yangxt90/articles/9021530.html http: ...

  6. Android平台下渗透测试工具大集合

    Android平台下渗透测试工具大集合 分享一个google的项目,各种Android下的渗透测试工具. Ad Network Detector (1.2): http://market.androi ...

  7. virtual box 安装 centos 7 不能上网问题解决总结

    http://blog.csdn.net/u013264730/article/details/51146359 1.设置virtualbox 网络选项 [root@centos1 ~]# cat / ...

  8. vc++6 Platform SDK February 2003

    vc++6.0 sp6 ftp://ejiasoft:softejia@ejia2.tust.edu.cn/else/VC++.6.0.with.SP6.ISO MSDN http://ftp.sds ...

  9. struts2逻辑视图类型汇总与解释(转)

    在struts2框架中,当action处理完之后,就应该向用户返回结果信息,该任务被分为两部分:结果类型和结果本身. 结果类型提供了返回给用户信息类型的实现细节.结果类型通常在Struts2中就已预定 ...

  10. static才是对代码的提升

    static才是对代码的提升 static的作用有如下三条: 1):隐藏. 当编译多个文件时,所有未加static前缀的全局变量和函数都具有全局可见性. 一个是a.c,另一个是main.c. 下面是a ...