【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 ...
随机推荐
- Ubuntu18.04下安装Sublime Text3并解决不能输入中文
Ubuntu18.04下安装Sublime Text3并解决不能输入中文! 废话不多说,直接按顺序执行下面命令开始安装! wget -qO - https://download.sublimetext ...
- loj #6342. 跳一跳 期望dp
令 $f[i]$ 表示已经到达 $i$ 点,为了到大 $n$ 点还期望需要的时间,随便转移一下就行. 由于本题卡空间,要记得开滚动数组. #include <bits/stdc++.h> ...
- __try __except与__try __finally的嵌套使用以及__finally的调用时机
原文:https://blog.csdn.net/SwordArcher/article/details/82465522 try-finally语句的语法与try-except很类似,稍有不同的是, ...
- linux安装sox,踩过坑的方法
参考文章 : https://blog.csdn.net/e_zhiwen/article/details/80037476 重新在源码中 执行一遍 ./configure --prefix=$HOM ...
- Linux安装配置JDK与卸载
最近在Linux系统部署门户,安装Oracle,导入dmp,JDK等等,遇到一大堆问题,解决后特有一番小小成就感,哇哈哈!在这里记录一下遇到问题: 官网下载JDK压缩包( .tar.gz ):http ...
- 洛谷P1714切蛋糕
题目 该题目就是求这n个数的前缀和所组成的数组的所有子区间的左端点和右端点相差不超过m,且他们的前缀和差最大,求出这个最大值即可. 而朴素算法肯定会T,而我们发现如果前缀和最大的话,则前缀和的值一定是 ...
- opendir,readdir,closedir
结构体dirent: struct dirent { ino_t d_ino; //inode number off_t d_off; //offset to the next diret unsi ...
- Requests库的主要方法:requests.request为requests.get和requests.post两个的汇总,只是需要传方法
1. requests.request(method,url,**kwargs) method:请求方式,对应get/put/post等七种 :拟获取页面的url链接 :控制访问参数,共13个 met ...
- mac编译Cpython
源代码中有什么? CPython 源代码分发包含各种工具,库和组件.我们将在本文中探讨这些内容. 首先,我们将重点关注编译器.先从 git 上下载 Cpython 源代码. git clone htt ...
- 如何提高工具开发和数据分析的效率?| jupyter | Rstudio server
这部分是超级干货,也能直接体现一个开发分析者的能力. 主要分为两部分: 1. 面对新问题时,如何高效的分析和开发? 2. 面对相似的问题时,如何最快时间的利用之前的开发经验? 因为现在我主要用shel ...