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 ...
随机推荐
- 2018-8-16JWTtoken用户登录认证思路分析9502751
2018-8-16JWTtoken用户登录认证思路分析9502751 JWT token在商城中的实现 class UserView(CreateAPIView): serializer_class ...
- RWA风险加权资产
风险加权资产(risk-weightedassets,简称RWA)是指对银行的资产加以分类,根据不同类别资产的风险性质确定不同的风险系数,以这种风险系数为权重求得的资产. 分为权重法和内评法. 内评法 ...
- kvm部署
第一:安装前准备 vmware workstation的虚拟机做kvm实验,需要开启嵌套虚拟化 1.首先在物理机BIOS设置里开启虚拟化功能 2.其次需要在vm里面开启一下两个功能,(关闭虚拟机勾选即 ...
- poj1328 Radar Installation(贪心 策略要选好)
https://vjudge.net/problem/POJ-1328 贪心策略选错了恐怕就完了吧.. 一开始单纯地把island排序,然后想从左到右不断更新,其实这是错的...因为空中是个圆弧. 后 ...
- 给已经存在的项目添加git
1.打开终端,cd到已存在项目的目录 2.输入以下命令行,初始化一个本地仓库: git init 3.输入以下命令,把工程所有文件都添加到该仓库中(千万别忘记后面的.号!!!): git ...
- 通过脚本调用MSBuild编译项目时指定Configuration(解決方案配置)和Platform(解決方案平台),Rebuid(重新生成解决方案),Clean(清理解决方案)
为了方便打包测试,自己PowerShell写了一个编译和发布的脚本,调用msbuild通过命令行来编译当前解决方案 后来发现一个问题,用VS编译解决方案,我通过 项目属性-Build设置 Releas ...
- Golang LicenseServer授权服务器的设计 与 RSA 密钥对的应用
//TODO 待写文章 目录: 1.为什么要写授权服务器 LicenseServer 2.授权服务器的设计思路 3.授权服务器所使用到的加密技术 1.为什么要写授权服务器 为了防止别人拿到二进制后, ...
- 08、共享变量(Broadcast Variable和Accumulator)
共享变量工作原理 Spark一个非常重要的特性就是共享变量. 默认情况下,如果在一个算子的函数中使用到了某个外部的变量,那么这个变量的值会被拷贝到每个task中.此时每个task只能操作自己的那份 ...
- 转sql server新增、修改字段语句(整理)
添加字段的SQL语句的写法: 通用式: alter table [表名] add [字段名] 字段属性 default 缺省值 default 是可选参数增加字段: alter table [表名] ...
- 每天一个linux命令:chmod
1.命令简介 chmod(Change mode) 用来将每个文件的模式更改为指定值.Linux/Unix 的档案调用权限分为三级 : 档案拥有者.群组.其他. u :目录或者文件的当前的用户 g : ...