problem

868. Binary Gap

solution1:

class Solution {
public:
int binaryGap(int N) {
int res = ;
vector<int> pos;
int k = ;
while(N>)
{
if(N&==) pos.push_back(k);
k++;
N >>=;//errr...
}
for(int i=; i<pos.size(); ++i)
{
res = max(res, pos[i]-pos[i-]);
}
return res;
}
};

solution2:

class Solution {
public:
int binaryGap(int N) {
int res = , last = -, k = ;
while(N>)
{
if(N&==)
{
if(last>=) res = max(res, k-last);
last = k;
}
N>>=;//errr...
k++;
}
return res;
}
};

参考
1. Leetcode_easy_868. Binary Gap;

2. Grandyang;

【Leetcode_easy】868. Binary Gap的更多相关文章

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

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

  2. 【Leetcode_easy】704. Binary Search

    problem 704. Binary Search solution: class Solution { public: int search(vector<int>& nums ...

  3. 【Leetcode_easy】693. Binary Number with Alternating Bits

    problem 693. Binary Number with Alternating Bits solution1: class Solution { public: bool hasAlterna ...

  4. 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)

    [LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...

  5. 【LeetCode】Validate Binary Search Tree ——合法二叉树

    [题目] Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defin ...

  6. 【LeetCode】Balanced Binary Tree 解题报告

    [题目] Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bi ...

  7. 【LeetCode】145. Binary Tree Postorder Traversal

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/binary-tree-pos ...

  8. 【LeetCode】二叉查找树 binary search tree(共14题)

    链接:https://leetcode.com/tag/binary-search-tree/ [220]Contains Duplicate III (2019年4月20日) (好题) Given ...

  9. 【leetcode】 Unique Binary Search Trees (middle)☆

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

随机推荐

  1. 五.划分LVM逻辑卷

    作用:    1.整合分散的空间    2.空间可以进行扩大   零散空闲存储 ---- 整合的虚拟磁盘 ---- 虚拟的分区   由众多的物理卷(PV)组合成卷组(VG),从卷组中划分多个逻辑卷(L ...

  2. Web API系列(二) Filter的使用以及执行顺序

    在WEB Api中,引入了面向切面编程(AOP)的思想,在某些特定的位置可以插入特定的Filter进行过程拦截处理.引入了这一机制可以更好地践行DRY(Don’t Repeat Yourself)思想 ...

  3. 繁繁的数字 背包DP

    繁繁的数字 背包DP 问一个数\(n\)有多少种二进制分解方案数 \(n\le 10^5\) 如7有7=4+2+1=4+1+1+1=2+2+2+1=2+2+1+1+1=2+1+1+1+1+1=1+1+ ...

  4. 力扣50题 Pow(x,n)

    本题是力扣网第50题. 实现 pow(x, n) ,即计算 x 的 n 次幂函数. 采用递归和非递归思路python实现. class Solution: #递归思路 def myPow_recurs ...

  5. esp8266 + dht11 + 两路继电器 实现pc远程控制开关机温度监控.并配置zabbix监控

    事因:翻了翻自己之前的硬件小箱子,几年前买的一些小东西,想用用起来. 正好我有些数据放在机器上,有时候需要机器启动,我使用完成后在断开. 其实网络唤醒也能做到,但是机器一直给电也不好,在说家里有小孩A ...

  6. Python逆向(一)—— 前言及Python运行原理

    一.前言 最近在学习Python逆向相关,涉及到python字节码的阅读,编译及反汇编一些问题.经过长时间的学习有了一些眉目,为了方便大家交流,特地将学习过程整理,形成了这篇专题.专题对python逆 ...

  7. php 运算符的优先级

    由上到下,依次递减,同行优先级相同. 结合方向 运算符 附加信息 无 clone new clone 和 new 左 [ array() 右 ** 算术运算符 右 ++ -- ~ (int) (flo ...

  8. C++2.0新特性(四)——<decltype、lambda>

    一.关键字decltype 引入新关键字decltype可以让编译器找出表达式的类型,为了区别typeof,以下做一个概念区分: typeof 是一个一元运算,放在一个运算数之前,运算数可以是任意类型 ...

  9. Kali Linux硬盘扩容

    传送门--->http://www.kali.org.cn/thread-27079-1-1.html.kali虚拟机扩容

  10. Leet Code 771.宝石与石头

    Leet Code编程题 希望能从现在开始,有空就做一些题,自己的编程能力太差了. 771 宝石与石头 简单题 应该用集合来做 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S  ...