【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 ...
随机推荐
- MySQL读写分离项目配置
一.项目信息 1.拓扑 二.环境规划 1.主机信息 2.软件信息 3.MySQL中间件 三.配置
- mysqldump 原理(转载)
mysqldump 备份过程可以描述为: (1) 先发出一条 flush tables 关闭实例上所有打开的表(2) 创建一个全局锁,FLUSH TABLES WITH READ LOCK获得 db ...
- Apache Phoenix系列 | 从入门到精通(转载)
原文地址:https://cloud.tencent.com/developer/article/1498057 来源: 云栖社区 作者: 瑾谦 By 大数据技术与架构 文章简介:Phoenix是一个 ...
- learning java 正则表达式
var regStr = "Java is very good"; Matcher m = Pattern.compile("\\w+").matcher(re ...
- WSAStartup() - 使用方法
当一个应用程序调用WSAStartup函数时, 操作系统根据请求的Socket版本来搜索相应的Socket库,然后绑定找到的Socket库到该应用程序中. 以后应用程序就可以调用所请求的Socket库 ...
- python使用ThreadPoolExecutor每秒并发5个
import time from concurrent.futures import ThreadPoolExecutor from functools import partial from log ...
- python技巧获取26个英语字母
import string string.ascii_uppercase # 获取26个大写字母 string.ascii_lowercase # 获取26个小写字母 string.ascii_let ...
- Android根据内网外网连接情况配置服务器访问IP
新项目的app,可通过内网和外网的服务器ip进行请求访问,但是客户提供了专业终端,终端在wifi情况下走外网内网都可以,但关闭wifi则只能走4G专网,也就是只能走内网. 可前往我的小站查看:Andr ...
- Mysql 查看所有线程,被锁的表等
## 查看所有MYSQl相关的线程 > show full processlist; ## 杀死线程id为2的线程 > kill 2 ## 查看服务器状态 > show status ...
- Spring boot 去除URL 里的 JSESSIONID
方法一 application.yml 里设置 server: port: 80 servlet: session: tracking-modes: cookie cookie: http-only: ...