LeetCode.868-二进制距离(Binary Gap)
这是悦乐书的第333次更新,第357篇原创
01看题和准备
今天介绍的是LeetCode算法题中Easy级别的第203题(顺位题号是868)。给定正整数N,找到并返回N的二进制表示中两个连续1之间的最长距离。如果没有连续两个1,则返回0。例如:
输入:22
输出:2
说明:22的二进制是10110。在22的二进制表示中,有三个1,第一对连续的1距离为2,第二对1的距离为1,答案是这两个距离中最大的一个,即2。
输入:5
输出:2
说明:5的二进制是101。
输入:6
输出:1
说明:6的二进制是110。
输入:8
输出:0
说明:8的二进制是1000。在二进制表示为8时没有任何连续的1,所以我们返回0。
注意:
- 1 <= N <= 10 ^ 9
02 第一种解法
题目的意思是计算一个二进制数中每对1的最长距离,若是只有一个1,距离则为0。
因此,我们先将N转为一个二进制字符串,然后去遍历字符串中的字符,使用一个临时变量存储前一个1的索引值,遇到新的1时就计算彼此之间的距离,取最大值,最后输出距离。
public int binaryGap(int N) {
String str = Integer.toBinaryString(N);
int prev = -1, distance = 0;
for (int i=0; i<str.length(); i++) {
if (str.charAt(i) == '1') {
if (prev == -1) {
prev = i;
} else {
distance = Math.max(distance, i-prev);
prev = i;
}
}
}
return distance;
}
03 第二种解法
如果不借助包装类Integer的转换二进制字符串方法,还有其他方式可以解吗?
可以,使用位运算即可。
使用右移位运算,可以得到其二进制数最后一位数,判断是1还是0,可以使用与运算,与运算的规则是相同位上为1就为1,0&1 = 0,1&1=1。剩下的就是计算最长距离了,还是使用一个临时变量存储前一次的1,最后输出最长距离。
public int binaryGap(int N) {
int prev = -1, distance = 0;
for (int i=0; i<32; i++) {
if (((N>>i)&1) == 1) {
if (prev != -1) {
distance = Math.max(distance, i-prev);
}
prev = i;
}
}
return distance;
}
04 第三种解法
和上面第二种解法一样的思路,只是将右移位运算、与运算拆分成两步来完成,循环也换成了while循环,其他处理思路没变。
public int binaryGap(int N) {
int prev = -1, distance = 0, i = 0;
while (N != 0) {
if ((N&1) == 1) {
if (prev != -1) {
distance = Math.max(distance, i-prev);
}
prev = i;
}
i++;
N >>= 1;
}
return distance;
}
05 小结
算法专题目前已连续日更超过六个月,算法题文章203+篇,公众号对话框回复【数据结构与算法】、【算法】、【数据结构】中的任一关键词,获取系列文章合集。
以上就是全部内容,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、转发就是对我最大的回报和支持!
LeetCode.868-二进制距离(Binary Gap)的更多相关文章
- Leetcode 868. 二进制间距
868. 二进制间距 显示英文描述 我的提交返回竞赛 用户通过次数201 用户尝试次数220 通过次数207 提交次数396 题目难度Easy 给定一个正整数 N,找到并返回 N 的二进制表示中 ...
- [Swift]LeetCode868. 二进制间距 | Binary Gap
Given a positive integer N, find and return the longest distance between two consecutive 1's in the ...
- Binary Gap(二进制空白)
中文标题[二进制空白] 英文描述 A binary gap within a positive integer N is any maximal sequence of consecutive zer ...
- # Leetcode 67:Add Binary(二进制求和)
Leetcode 67:Add Binary(二进制求和) (python.java) Given two binary strings, return their sum (also a binar ...
- 【Leetcode_easy】868. Binary Gap
problem 868. Binary Gap solution1: class Solution { public: int binaryGap(int N) { ; vector<int&g ...
- [leetcode] (周赛)868. 二进制间距
868. 二进制间距 读懂题意就出来了 class Solution { public int binaryGap(int N) { String s = Integer.toBinaryString ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- [LeetCode] Serialize and Deserialize Binary Tree 二叉树的序列化和去序列化
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- LeetCode:Maximum Depth of Binary Tree_104
LeetCode:Maximum Depth of Binary Tree [问题再现] Given a binary tree, find its maximum depth. The maximu ...
随机推荐
- GetShortPathName函数
Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathName" (ByVal ...
- (转) IntelliJ IDEA2018激活
IntelliJ IDEA2018破解教程 破解方法:下载破解补丁→修改配置文件→输入激活码→激活成功 由于JetBrains封杀,大部分激活服务器已经不能使用,使用下面的比较麻烦的方法也可以进行破解 ...
- CF Round #424 Div.2 D
n个人拿K个钥匙中的n个然后到办公室(点p) 问最少需要的时间是多少 先排序 如果j<=i 则必须拿这个钥匙 dp[i][j]=max(dp[i-1][j-1],abs(p-b[j])+abs( ...
- nginx代理证书使用方法
一.证书购买 一般情况,申请证书时需要添加DNS解析,具体的步骤一般运营商都会给予详细说明.当然,也需要填写证书保护的处理的域名,一般非免费的证书可以支持多个域名处理,免费的一般只能支持一个域名的设置 ...
- Docker(五):Dockerfile
我们使用 Dockerfile 定义镜像,依赖镜像来运行容器,因此 Dockerfile 是镜像和容器的关键,Dockerfile 可以非常容易的定义镜像内容,同时在我们后期的微服务实践中,Docke ...
- js对对象增加删除属性
1.首选创建一个对象 var a={}; 2.然后对这个对象赋值 a.name='zhouy';console.log(a);var age="age";a[age]=26;con ...
- js 反斜杠 处理
var t = jsonstr.replace(/\\/g,"\\\\\\\\"); --\\ 表示 代码 输出 \' 单引号 \" 双引号 \& 和号 \\ 反 ...
- CF Round #569 Div2(contest1180)
比赛链接:http://codeforces.com/contest/1180 Problem A 题意:给出n,问方块数.看图理解... Solution: 找一找规律就可以了,发现方块数为2n*( ...
- docker安装xxl-job
一 安装mysql root@localhost tmp]# docker run -itd --name xxl-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD= ...
- 损坏的RAID5
损坏的RAID5 string讀入卡cin 関同步 ios::sync_with_stdio(false) 由塊號映射到具體位置 塊號id對應第col個字符串 字符串開始的位置st #include& ...