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 ...
随机推荐
- Python 列表改为嵌套列表再转为矩阵
最终目的--------->[下面的形式我就可以直接转为矩阵,用mat即可] 下午为了实现list的这个功能,查找了好多库,但是都没有找到怎么做,索性就自己封装了一个函数实现了其功能: def ...
- plsql 工具怎样导出 oracle 表结构
一.双击 plsql 工具,输入登陆用户.登陆密码以及登陆数据库名称,如下图: 二.菜单 Tools --> Export User Objects...,如下图: 三.进入导出界面后,可以选择 ...
- python之函数深入探测
第一:命名空间与作用域 命名空间: 局部命名空间: def foo(): x=1 def func(): pass 全局命名空间: import time class ClassName:pass d ...
- flask之基于DBUtils实现数据库连接池、本地线程、上下文
本篇导航: 数据库连接池 本地线程 上下文管理 面向对象部分知识点解析 1.子类继承父类__init__的三种方式 class Dog(Animal): #子类 派生类 def __init__(se ...
- Python常用的软件包
下面是Python开发常用的软件包. 名称 用途 安装命令 opengl sudo pip3 install PyOpenGL pyqtgraph GUI图形库 sudo pip3 instal ...
- java 规则引擎资料汇集
1. ibm的developworks中较早的一篇关于规则引擎的文章 https://www.ibm.com/developerworks/cn/java/j-java-rules/ 2. 一篇硕士论 ...
- Android的Databinding-资源绑定
databinding还能对布局的资源文件进行绑定. <data class="ResourceBinding"> <variable name="la ...
- mssql f_Split
mssql可以如下CREATE FUNCTION [dbo].[f_Split] ( @val varchar(max),@Splits varchar(100))RETURNS @Table TAB ...
- ClickHouse高可用集群的配置
上一篇文章写过centos 7下clickhouse rpm包安装和基本的目录结构,这里主要介绍clickhouse高可用集群的部署方案,因为对于默认的分布式表的配置,每个分片只有一份,这样如果挂掉一 ...
- Unity应用架构设计(10)——绕不开的协程和多线程(Part 1)
在进入本章主题之前,我们必须要了解客户端应用程序都是单线程模型,即只有一个主线程(Main Thread),或者叫做UI线程,即所有的UI控件的创建和操作都是在主线程上完成的.而服务器端应用程序,也就 ...