LeetCode - 868. Binary Gap
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的更多相关文章
- LeetCode 868 Binary Gap 解题报告
题目要求 Given a positive integer N, find and return the longest distance between two consecutive 1's in ...
- 【Leetcode_easy】868. Binary Gap
problem 868. Binary Gap solution1: class Solution { public: int binaryGap(int N) { ; vector<int&g ...
- 【LeetCode】868. Binary Gap 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 线性扫描 日期 题目地址:https://leetc ...
- [LeetCode&Python] Problem 868. Binary Gap
Given a positive integer N, find and return the longest distance between two consecutive 1's in the ...
- [LeetCode] 868. Binary Gap_Easy
Given a positive integer N, find and return the longest distance between two consecutive 1's in the ...
- leetcode 199 :Binary Tree Right Side View
// 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...
- 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 ...
- LeetCode:Unique Binary Search Trees I II
LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...
- LeetCode: Validata Binary Search Tree
LeetCode: Validata Binary Search Tree Given a binary tree, determine if it is a valid binary search ...
随机推荐
- win7 64位安装Dlib19.6版本的过程记录
本文为原创,未经允许不得转载. 1.去Dlib的官网下载dlib-19.6的源文件.然后解压到Myprograms下的Res文件夹下 2.到CMake的官网下载Cmake,我下载以后解压,然后进入到b ...
- HTML常用标签1
1 html html:超文本标记(标签)语言 通过标签语言来标记要显示的网页中的各个部分.一套规则,浏览器认识的规则 浏览器渲染的顺序:从上到下,从左到右 对于不同的浏览器,对同一标签可能会有不完全 ...
- spring跨域问题
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Co ...
- Netty buffer缓冲区ByteBuf
Netty buffer缓冲区ByteBuf byte 作为网络传输的基本单位,因此数据在网络中进行传输时需要将数据转换成byte进行传输.netty提供了专门的缓冲区byte生成api ByteBu ...
- SpringMVC的拦截器(Interceptor)和过滤器(Filter)的区别与联系
摘自: http://blog.csdn.net/xiaoyaotan_111/article/details/53817918 一 简介 (1)过滤器: 依赖于servlet容器.在实现上基于函数回 ...
- mysql忘记密码时如何修改密码
1.首先关闭mysql服务 2.进入mysql安装目录,我的是在C:\Program Files\MySQL\MySQL Server 5.5\bin 3.dos命令行执行:mysqld -nt -- ...
- 【纵谭 Python】系列直播(持续更新)
老周最近录了一些跟 Python 有关的直播,可以在“一直播”中搜索 ID 号 139251129 关注,也可以在微博中查看,反正都一样,同步的. 第一集:简单胡扯一下相关环境搭建.安装 Python ...
- C# System.IO.StreamReader
实现一个 TextReader,使其以一种特定的编码从字节流中读取字符. using System; using System.IO; class Test { public static void ...
- 一些Android手机的平台信息
1.OPPO A83 (2018-04-16) A83:/ $ cat /system/build.prop | grep "product"# ro.product.cpu.ab ...
- 硬盘SMART检测参数详解[转]
一.SMART概述 要说Linux用户最不愿意看到的事情,莫过于在毫无警告的情况下发现硬盘崩溃了.诸如RAID的备份和存储技术可以在任何时候帮用户恢复数据,但为预防硬件崩溃造成数据丢失所 ...