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 ...
随机推荐
- Spring Framework Part3 IoC and Dynamic Proxy
spring serious of blog edit by 马士兵教育 Maven方式创建Spring工程 工程创建 1.新建项目 选择Maven Project 2.勾选 Create a sim ...
- Jam's balance HDU - 5616 (01背包基础题)
Jim has a balance and N weights. (1≤N≤20) The balance can only tell whether things on different side ...
- MYSQL利用merge存储引擎来实现分表
创建user1和user2两个分表 建表语句如下:只是表名不一样,其他字段信息及主键一致. CREATE TABLE IF NOT EXISTS user1( id INT(11) NOT NUL ...
- 界面开发工具集Telerik UI for ASP.NET AJAX发布R2 2019
Telerik UI for ASP.NET AJAX拥有构建ASP.NET AJAX和SharePoint应用程序的80+控件,将为任何浏览器和设备构建Web Forms应用程序的时间缩短一半.Te ...
- yang学习
Ø Yang 是对NETCONF数据模型.操作进行建模,覆盖NETCONF协议的操作层和内容层 Ø 模块和子模块 模块是yang中定义的基本单位. 模块定义单个数据模型,它可以定义完整的聚合模型, ...
- vue项目history模式下微信分享相关问题
import wx from '@/utils/wx' import { shareApi } from '@/api' // 微信验证 export function requireConfig() ...
- conda Pyhon版本切换
1.首先确保你的系统里已经安装了Conda,打开命令行窗口,执行命令:conda --version 2.查看你的系统当前已有的Python环境,执行命令:conda info --envs,从图中我 ...
- git的用法的总结
git的基本用法(一) 1).git的初始化 --配置用户名和邮箱 不然后面无法提交 git config --global user.name "myname"; git con ...
- 游标定位:Cursor类
关于 Cursor Cursor 是每行的集合. 使用 moveToFirst() 定位第一行. 你必须知道每一列的名称. 你必须知道每一列的数据类型. Cursor 是一个随机的数据源. 所有的数据 ...
- sh_02_判断年龄改进版
sh_02_判断年龄改进版 # 输入用户年龄 age = int(input("请输入年龄:")) # 判断是否满 18 岁 (>=) if age >= 18: # ...