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. Struts11---文件上传

    01.创建对应的上传页面 <body> <form action="user/upload" method="post" enctype=&q ...

  2. LeetCode OJ:Remove Duplicates from Sorted Array II(移除数组中的重复元素II)

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  3. java中商业数据计算时用到的类BigDecimal和DecimalFormat

    1.引言 借用<Effactive Java>这本书中的话,float和double类型的主要设计目标是为了科学计算和工程计算.他们执行二进制浮点运算,这是为了在广域数值范围上提供较为精确 ...

  4. canvas - 柱子效果

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  5. .Net版SQLite无法访问网络位置的数据库文件-winOpen,os_win.c 36702异常

    最近一个C#小程序,希望将SQLite数据库放在网络共享的位置,让多个客户端同时访问.却发现SQLite连接不上该网络位置的数据库,而如果数据库在本地则一切正常. 例如将SQLite数据库 test. ...

  6. auto_ptr, unique_ptr, shared_ptr and weak_ptr智能指针讲解

    笔者介绍:姜雪伟,IT公司技术合伙人,IT高级讲师,CSDN社区专家,特邀编辑,畅销书作者,已出版书籍:<手把手教你架构3D游戏引擎>电子工业出版社和<Unity3D实战核心技术详解 ...

  7. BZOJ - 3622:已经没有什么好害怕的了 (广义容斥)

    [BZOJ3622]已经没有什么好害怕的了 Description Input Output Sample Input 4 2 5 35 15 45 40 20 10 30 Sample Output ...

  8. GlassFish的安装与使用(Windows)

    前言 Glassfish是一款由Sun公司开发的(现由甲骨文公司赞助)开源的免费的应用服务器,它既是EJB容器也是WEB容器.Glassfish支持最新版的Java EE标准. Glassfish与T ...

  9. rpm -Uvh 升级时的陷阱

    问题现象 用rpm -Uvh升级后,原先的一个软链接被删除了,而采用先rpm -e 卸载rpm包,再rpm -ivh 安装包的方法,这个软链接还在.这个软链接是在rpm包安装的时候建立,也只有在rpm ...

  10. 系列文章--WCF后传学习文章

    WCF后传系列(10):消息处理功能核心 摘要: WCF是一个通信框架,同时也可以将它看成是一个消息处理或者传递的基础框架,它可以接收消息.对消息做处理,或者根据客户端给定的数据构造消息并将消息发送到 ...