【Leetcode_easy】868. Binary Gap
problem
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的更多相关文章
- 【LeetCode】868. Binary Gap 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 线性扫描 日期 题目地址:https://leetc ...
- 【Leetcode_easy】704. Binary Search
problem 704. Binary Search solution: class Solution { public: int search(vector<int>& nums ...
- 【Leetcode_easy】693. Binary Number with Alternating Bits
problem 693. Binary Number with Alternating Bits solution1: class Solution { public: bool hasAlterna ...
- 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)
[LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...
- 【LeetCode】Validate Binary Search Tree ——合法二叉树
[题目] Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defin ...
- 【LeetCode】Balanced Binary Tree 解题报告
[题目] Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bi ...
- 【LeetCode】145. Binary Tree Postorder Traversal
Difficulty: Hard More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/binary-tree-pos ...
- 【LeetCode】二叉查找树 binary search tree(共14题)
链接:https://leetcode.com/tag/binary-search-tree/ [220]Contains Duplicate III (2019年4月20日) (好题) Given ...
- 【leetcode】 Unique Binary Search Trees (middle)☆
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
随机推荐
- How To Set The Hostname On Ubuntu Or Debian?
$ sudo hostnamectl set-hostname your-hostname $ sudo vim /etc/hosts Open the hosts file and add the ...
- 洛谷 P2296 寻找道路 题解
每日一题 day42 打卡 Analysis 首先,预处理,把每条边反向. 从终点开始bfs,标记从终点开始可以走到的点. 第二步,枚举每一个点,如果这个点没有被标记,则枚举它的每一条出边(反向后的) ...
- #505. 「LibreOJ β Round」ZQC 的游戏
题目描述 首先一定是让ZQC吃掉他能吃到的所有的球,这样才能尽可能的满足ZQC的质量是所有玩家中最大的. 在满足某一个玩家的质量不会超过ZQC的情况下,让这个玩家吃掉尽可能多的球,让其他玩家吃掉的尽可 ...
- synology git 服务器问题处理
synology git 服务器问题处理 安装 synology 上的 git 套件, 发现使用过程中存在很多问题. permission 问题 ## 将对应的目录设为git所有者 chown git ...
- 2019强网杯web upload writeup及关键思路
<?phpnamespace app\web\controller; class Profile{ public $checker; public $filename_tmp; ...
- [提权]sudo提权复现(CVE-2019-14287)
2019年10月14日, sudo 官方在发布了 CVE-2019-14287 的漏洞预警. 0x00 简介 sudo 是所有 unix操作系统(BSD, MacOS, GNU/Linux) 基本集成 ...
- C平衡二叉树(AVL)创建和删除
AVL是最先发明的自平衡二叉查找树算法.在AVL中任何节点的两个儿子子树的高度最大差别为一,所以它也被称为高度平衡树,n个结点的AVL树最大深度约1.44log2n.查找.插入和删除在平均和最坏情况下 ...
- sql server for centos7
sql server for centos7 笔者在CENTOS7上面安装SQL SERVER,感觉非常方便. 但有一点要注意,字段是字符串类型的,要使用nvarchar(),不能使用varchar( ...
- 4 个用于执行高级数学计算的 JavaScript 库
在使用JavaScript执行数学方面的任务时,往往要用到浮点运算,且需要精确到某位小数,这就容易造成错误,而且会相当费时.因此,如果你需要做一些高精度的数学计算的编程工作,比如财务或科学计算,那么你 ...
- Java-Maven(十):Maven 项目常用plugins
本文主要总结最近一段时间使用maven时,遇到需要maven plugins的一些简单总结. 1)在Build下重新指定最终打包报名 <build> <!--最终打包的包名,如果这里 ...