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.

 

Example 1:

Input: 22
Output: 2
Explanation:
22 in binary is 0b10110.
In the binary representation of 22, there are three ones, and two consecutive pairs of 1's.
The first consecutive pair of 1's have distance 2.
The second consecutive pair of 1's have distance 1.
The answer is the largest of these two distances, which is 2.

Example 2:

Input: 5
Output: 2
Explanation:
5 in binary is 0b101.

Example 3:

Input: 6
Output: 1
Explanation:
6 in binary is 0b110.

Example 4:

Input: 8
Output: 0
Explanation:
8 in binary is 0b1000.
There aren't any consecutive pairs of 1's in the binary representation of 8, so we return 0.

Note:

  • 1 <= N <= 10^9
class Solution {
public int binaryGap(int N) {
int cnt = -32, ret = 0;
while (N > 0) {
if ((N & 1) == 1) {
ret = Math.max(ret, cnt);
cnt = 0;
}
N = N >> 1;
cnt++;
}
return ret;
}
}

LeetCode - 868. Binary Gap的更多相关文章

  1. LeetCode 868 Binary Gap 解题报告

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

  2. 【Leetcode_easy】868. Binary Gap

    problem 868. Binary Gap solution1: class Solution { public: int binaryGap(int N) { ; vector<int&g ...

  3. 【LeetCode】868. Binary Gap 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 线性扫描 日期 题目地址:https://leetc ...

  4. [LeetCode&Python] Problem 868. Binary Gap

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

  5. [LeetCode] 868. Binary Gap_Easy

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

  6. leetcode 199 :Binary Tree Right Side View

    // 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...

  7. LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal

    LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...

  8. LeetCode:Unique Binary Search Trees I II

    LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...

  9. LeetCode: Validata Binary Search Tree

    LeetCode: Validata Binary Search Tree Given a binary tree, determine if it is a valid binary search ...

随机推荐

  1. Java中的RASP实现

    RSAP RASP是Gartner公司提出的一个概念,称:程序不应该依赖于外部组件进行运行时保护,而应该自身拥有运行时环境保护机制: RASP就是运行时应用自我保护(Runtime applicati ...

  2. 【贪心】LIS @The 15th Zhejiang Provincial Collegiate Programming Contest E

    传送门 题意要你构造一个序列,使得该序列的每个位置上的最长上升子序列的长度能构成给定的序列. 构造序列的元素要求还要求满足给定的上下界 solution 我们可以把给出的最长上升子序列的长度按升序排列 ...

  3. PowerBI发布到网页

    如果网页当中需要嵌入PowerBI的报表,可以在PowerBI当中生成链接,然后网页或者博客当中插入这一段html代码. 以下是PowerBI生产网页链接的示例,并且在博客的最后也插入了PowerBI ...

  4. golang 对结构体进行格式化输出

    可以使用 `return fmt.Sprintf("%+v", *conf) ` 来打印结构体,包括结构体的key值.但是由于结构体内容较多,都在一行,所以希望可以格式化输出结构体 ...

  5. 运行VsCode缺少libxss.so.1

    安装libXScrnSaver即可 yum install libXScrnSaver     使用的时候出现一个错误 bash: /usr/local/bin/rar: /lib/ld-linux. ...

  6. Mac上搭建ELK

    转载自我的个人博客:http://blog.ywheel.cn/post/2017/03/04/setup_elk_on_mac/ 最近的项目需要对文本数据各字段进行快速检索.组合查询.模糊查询,在架 ...

  7. CUDA各版本官方下载地址

    一.CUDA各版本官方下载地址 地址:https://developer.nvidia.com/cuda-toolkit-archive 二.说明 备忘,平时找个版本太难找了.

  8. CentOS安装Navicat

    首先,下载文件navicat120_mysql_cs_x64.tar.gz,然后用命令 tar -zxvf 解压.解压以后,进入解压目录,运行start_navicat,就可以运行了. 运行以后,可能 ...

  9. Linear SVM和LR的区别和联系

    首先,SVM和LR(Logistic Regression)都是分类算法.SVM通常有4个核函数,其中一个是线性核,当使用线性核时,SVM就是Linear SVM,其实就是一个线性分类器,而LR也是一 ...

  10. redis详解(三)-- 面试题(转载)

    1. 使用redis有哪些好处? (1) 速度快,因为数据存在内存中,类似于HashMap,HashMap的优势就是查找和操作的时间复杂度都是O(1) (2) 支持丰富数据类型,支持string,li ...