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. 用LinkedList和ArrayList实现自定义栈的异同

    //ArrayList已连续的空间进行存储数据  //LinkedList已链表的结构存储数据    //栈  MyStark ms=new MyStark();//new 一个实现栈的类  //压栈 ...

  2. WinDbg 图形界面功能(一)

    当我们启动windbg后,我们就能看到Windbg的样子了,如下: 本部分讨论 WinDbg 图形用户界面的元素. 这些元素包括以下各项:菜单.工具栏和快捷键.菜单有:文件菜单.编辑菜单.视图菜单.调 ...

  3. Intel 8086 CPU

    一.8086概述 Intel8086拥有四个16位的通用寄存器,也能够当作八个8位寄存器来存取,以及四个16位索引寄存器(包含了堆栈指标).资料寄存器通常由指令隐含地使用,针对暂存值需要复杂的寄存器配 ...

  4. RookeyFrame 字典 新增和绑定

    原文:https://www.cnblogs.com/rookey/p/10856657.html 注意: 数据字典 -> 新增 把“是否生效”勾上 是否生效都要勾上哦 !!! 应该自动勾上才对 ...

  5. RookeyFrame 线下 添加Model

    1.在Model层添加一个类,继承BaseEntity,如: (将就demo里面的类改了一下) using Rookey.BusSys.Model.Base; using Rookey.BusSys. ...

  6. DIJ的优化,和spfa的优化

    SPFA和DIJ求最短路的算法的坑点一直是很多的.经常会让人搞不懂. 易错案例: 用重载运算符来排序,如: struct cmp { bool operator ()(int x, int y) co ...

  7. Android Studio导入google training example gradle失败

    Error:Unable to tunnel through proxy. Proxy returns "HTTP/1.1 400 Bad Request 每次从github的Google ...

  8. 6、httpd2.4 编译安装LAMP

    www.itjc8.com 新特性: MPM支持运营DSO机制(动态共享对象),以模块形式按需加载 支持event MPM 支持异步读写 支持每模块及每个目录分别使用各自的日志级别 每请求配置 增强版 ...

  9. 使用Android的日志工具Log

    Android中的日志工具类是Log,这个类中提供了5个方法来供我们打印日志 1.Log.v()用于打印那些最为琐碎的,意义最小的日志信息.对应级别verbose,是Android日志里面级别最低的一 ...

  10. mac使用crawlab

    这里采用docker-compose的 第一步拉镜像 docker pull tikazyq/crawlab:latest 第二步修改compose文件 docker-compose.yml文件 ve ...