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 ...
随机推荐
- 我来说说XML文件中的xmlns、xmlns:xsi和xsi:schemaLocation、dtd文件的具体含义
文章摘自:https://yq.aliyun.com/articles/40353 http://www.cnblogs.com/zhao1949/p/5652167.ht ...
- robotframework ride报错 Keyword 'BuiltIn.Log' expected 1 to 5 arguments, got 12.
错误原因,else和else if使用了小写,必须使用大写才能识别到.
- 找不到javax.
https://blog.csdn.net/q343509740/article/details/79515911 idea导入java工程 file --> new -->存在的工程
- [易学易懂系列|rustlang语言|零基础|快速入门|(7)|函数Functions与闭包Closure]
[易学易懂系列|rustlang语言|零基础|快速入门|(7)函数Functions与闭包Closure] 有意思的基础知识 函数Functions与闭包Closure 我们今天再来看看函数. 在Ru ...
- WPF 多个选项卡TabControl 页面分离
此项目源码下载地址:https://github.com/lizhiqiang0204/TabControl-page-separation 每个页面的按键处理事件直接对应该页面下的cs文件 Main ...
- 前端每日实战:140# 视频演示如何用纯 CSS 创作文本的淡入动画效果
效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/ZMwgqK 可交互视频 此视频是可 ...
- python之路day14--列表生成式、生成器generator、生成器并行
列表生成式 列表生成式阅读量: 44 现在有个需求,现有列表a=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],要求你把列表里的每个值加1,你怎么实现?你可能会想到2种方式 二逼青年 ...
- day_08 字符编码乱码处理
Python3默认编码是unicode:而Python2是ASCII码.Windows环境默认是gbk编码. 常见编码错误原因: 1. Python解释器的默认编码 2. Python源文件文件编码 ...
- mysql 查询碎片的方法
mysql 查询碎片的方法 mysql length,engine,data_free,table_rows group by table_name order by table_rows asc; ...
- Swiper 的引入
1. 从官网下载必要资源 https://www.swiper.com.cn/download/index.html#file1 2. 在项目中<head>中引入swiper.min.cs ...