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. YAML_07 有报错信息,告诉你错误忽略,继续执行下面的命令

    ansible]# vim user5.yml --- - hosts: cache   remote_user: root   vars:     user: bb   tasks:    - sh ...

  2. @RestController和@GetMapping

    @RestController 可以代替@Controller使用,使用了@RestController的控制器默认所有请求方法都用了@ResponseBody注解. @GetMapping(&quo ...

  3. sudo 命令

    su命令和su -命令最大的本质区别就是: 前者只是切换了root身份,但Shell环境仍然是普通用户的Shell: 而后者连用户和Shell环境一起切换成root身份了.只有切换了Shell环境才不 ...

  4. CodeForeces 842d Vitya and Strange Lesson ——(带lazy标记的01字典树)

    给一个序列,每次操作对这个序列中的所有数异或一个x,问每次操作完以后整个序列的mex值. 做法是去重后构建01字典树,异或x就是对root加一个x的lazy标志,每次pushDown时如果lazy的这 ...

  5. NAT反向转换基本配置详解

  6. php 502 无错误行和报错文件的情况下使用gdb调试方法

    lnmp环境 gdb  /usr/local/php5.2/bin/php-cgi  /tmp/coredump-php-cgi.20503 source /home/tmp/lnmp1.4-full ...

  7. 随机森林算法OOB_SCORE最佳特征选择

    RandomForest算法(有监督学习),可以根据输入数据,选择最佳特征组合,减少特征冗余:原理:由于随机决策树生成过程采用的Boostrap,所以在一棵树的生成过程并不会使用所有的样本,未使用的样 ...

  8. Python——graphviz及pydotplus安装步骤

    Python——graphviz及pydotplus安装步骤 一.安装Graphviz 网站:http://www.graphviz.org/download/ 下载msi文件 直接安装,完成之后添加 ...

  9. oracle tablespace pctfree pctused

    二.oracle pctfree和pctused详解 一.建立表时候,注意PCTFREE参数的作用 PCTFREE:为一个块保留的空间百分比,表示数据块在什么情况下可以被insert,默认是10,表示 ...

  10. https://blog.csdn.net/qq_35636311/article/details/78255568 cPython 扩展

    https://blog.csdn.net/qq_35636311/article/details/78255568 http://book.pythontips.com/en/latest/pyth ...