Leetcode868.Binary Gap二进制间距
给定一个正整数 N,找到并返回 N 的二进制表示中两个连续的 1 之间的最长距离。
如果没有两个连续的 1,返回 0 。
示例 1:
输入:22 输出:2 解释: 22 的二进制是 0b10110 。 在 22 的二进制表示中,有三个 1,组成两对连续的 1 。 第一对连续的 1 中,两个 1 之间的距离为 2 。 第二对连续的 1 中,两个 1 之间的距离为 1 。 答案取两个距离之中最大的,也就是 2 。
示例 2:
输入:5 输出:2 解释: 5 的二进制是 0b101 。
示例 3:
输入:6 输出:1 解释: 6 的二进制是 0b110 。
示例 4:
输入:8 输出:0 解释: 8 的二进制是 0b1000 。 在 8 的二进制表示中没有连续的 1,所以返回 0 。
提示:
- 1 <= N <= 10^9
class Solution {
public:
int binaryGap(int N) {
if(N <= 1)
return 0;
vector<int> bits;
while(N)
{
int temp = N % 2;
bits.push_back(temp);
N /= 2;
}
int len = bits.size();
int first = -1;
int second = -1;
int MAX = 0;
for(int i = 0; i < len; i++)
{
if(bits[i] == 1)
{
if(first == -1)
{
first = i;
}
else if(second = -1)
{
second = i;
MAX = max(MAX, second - first);
second = -1;
first = i;
}
}
}
return MAX;
}
};
Leetcode868.Binary Gap二进制间距的更多相关文章
- [LeetCode] Binary Gap 二进制间隙
Given a positive integer N, find and return the longest distance between two consecutive 1's in the ...
- Binary Gap(二进制空白)
中文标题[二进制空白] 英文描述 A binary gap within a positive integer N is any maximal sequence of consecutive zer ...
- Leetcode 868. 二进制间距
868. 二进制间距 显示英文描述 我的提交返回竞赛 用户通过次数201 用户尝试次数220 通过次数207 提交次数396 题目难度Easy 给定一个正整数 N,找到并返回 N 的二进制表示中 ...
- 【LeetCode-面试算法经典-Java实现】【067-Add Binary(二进制加法)】
[067-Add Binary(二进制加法)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given two binary strings, return thei ...
- 【Leetcode_easy】868. Binary Gap
problem 868. Binary Gap solution1: class Solution { public: int binaryGap(int N) { ; vector<int&g ...
- [leetcode] (周赛)868. 二进制间距
868. 二进制间距 读懂题意就出来了 class Solution { public int binaryGap(int N) { String s = Integer.toBinaryString ...
- [Swift]LeetCode868. 二进制间距 | Binary Gap
Given a positive integer N, find and return the longest distance between two consecutive 1's in the ...
- [LeetCode] Prime Number of Set Bits in Binary Representation 二进制表示中的非零位个数为质数
Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime ...
- 762. Prime Number of Set Bits in Binary Representation二进制中有质数个1的数量
[抄题]: Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a ...
随机推荐
- Java 使用模板生成 Word 文件---基于 Freemarker 模板框架
Java项目引入 Freemarker 插件自行完成. 步骤如下: .编写 Word 模板,并将模板中要用代码动态生成数据用 Freemarker 变量取代,即${变量名},如${username}: ...
- bzoj1706 relays 奶牛接力跑 线性代数
题目描述 FJ的N(2 <= N <= 1,000,000)头奶牛选择了接力跑作为她们的日常锻炼项目.至于进行接力跑的地点 自然是在牧场中现有的T(2 <= T <= 100) ...
- Putty保存设置
使用putty连接服务器每次都要配置编码,有时候忘了配置,提示就出现中文乱码真是坑爹(纯英文提示也行啊),好了闲话少说,步入正题. Putty的设置保存功能隐藏的实在太好了,原来在Connection ...
- Spring注解驱动开发(一)-----组件注册
注册bean xml方式 1.beans.xml-----很简单,里面注册了一个person bean <?xml version="1.0" encoding=" ...
- Delphi代码规范
1. 前言 本文档主要是为Delphi开发人员提供一个源代码书写标准,以及程序和文件的命名标准,使他们在编程时有一致格式可遵循.这样,每个编程人员编写的代码能够被其他人理解. 2. 源程序书写规范 2 ...
- IO流19(完) --- RandomAccessFile实现数据的插入 --- 技术搬运工(尚硅谷)
原hello.txt文件中的内容:abcdefghijklmn 想要实现的效果是,将xyz插入到abc后面,将文件内容变成:abcxyzdefghijklmn @Test public void te ...
- 爬虫(四)Selenium + Headless Chrome爬取Bing图片搜索结果
Bing图片搜索结果是动态加载的,如果我们直接用requests去访问页面爬取数据,那我们只能拿到很少的图片.所以我们使用Selenium + Headless Chrome来爬取搜索结果.在开始前, ...
- 使用 windows 批处理指令(BAT文件)进行压缩文件(zip)解压操作
以下指令包括文件删除.复制.zip文件解压操作.使用7z指令指令进行解压操作前,需要确保 windows 的 path 系统环境变量中存在7z的安装路径. 7z的下载地址:https://www.7- ...
- Docker(二)安装及常用命令
1.安装 1.安装虚拟机VMWare 链接:https://pan.baidu.com/s/1Xl7ENUm2gapPOFs-iXHpRQ 提取码:eubm 2.下载centos,我下的是这个版本的 ...
- 嘴巴题1 LA2531 足球联赛
LA2531 足球联赛 题目: 有n只队伍打比赛,给出每只队目前获胜和失败的场数,再给出两两队伍接下来的比赛场次,问你哪些队伍可能的冠军 (题面摘自http://blog.csdn.net/s_h_r ...