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 ...
随机推荐
- 动态规划——DP算法(Dynamic Programing)
一.斐波那契数列(递归VS动态规划) 1.斐波那契数列——递归实现(python语言)——自顶向下 递归调用是非常耗费内存的,程序虽然简洁可是算法复杂度为O(2^n),当n很大时,程序运行很慢,甚至内 ...
- 长按触发(PC端和移动端)
$.fn.longPress = function(fn) { var timeout = undefined; var $this = this; for(var i = 0;i<$this. ...
- 原生JS实现简易计算器
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- pixhawk 常见问题 持续更新
红灯蓝灯闪,初始化中,请稍等 黄灯双闪,错误,系统拒绝解锁 黄灯闪,遥控器关闭 黄灯快速闪且滴滴响,电池保护激活 蓝灯... 未见过.... 绿灯闪: 已加锁,GPS锁定已获得. 准备解锁. 从加锁状 ...
- mapreduce join操作
上次和朋友讨论到mapreduce,join应该发生在map端,理由太想当然到sql里面的执行过程了 wheremap端 join在map之前(笛卡尔积),但实际上网上看了,mapreduce的笛卡尔 ...
- mysqldump与mydumper
mydumper -u root -S /srv/my3308/run/mysql.sock -B trade_platform -o /data/trade_platform
- 关于dex 64K 引用限制
1.官方文档 https://developer.android.com/studio/build/multidex 主要内容: 什么是64K限制 编码时如何避免64K 限制 拆分dex避免64K 限 ...
- Django项目:CRM(客户关系管理系统)--35--27PerfectCRM实现King_admin编辑复选框
#admin.py # ————————01PerfectCRM基本配置ADMIN———————— from django.contrib import admin # Register your m ...
- 读书笔记--iBATIS in Action 目录
1.iBATIS的理念 2.iBATIS是什么 3.安装和配置iBATIS 4.使用以映射语句 5.执行非查询语句 6.使用高级查询技术 7.事务 8.使用动态SQL 9.使用高速缓存提高性能 10. ...
- 读书笔记--Head First Ajax 目录
1.使用Ajax 2.设计Ajax 3.javascripte事件 4.多个事件处理程序 5.异步应用 6.文档对象模型 7.管理DOM 8.框架与工具包 9.xml请求与响应 10.json 11. ...