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 ...
随机推荐
- Java并发编程实战 第15章 原子变量和非阻塞同步机制
非阻塞的同步机制 简单的说,那就是又要实现同步,又不使用锁. 与基于锁的方案相比,非阻塞算法的实现要麻烦的多,但是它的可伸缩性和活跃性上拥有巨大的优势. 实现非阻塞算法的常见方法就是使用volatil ...
- angularjs 代码结构两种写法
1.当路由中不写controller的时候,controller写在对应的html表单中 2.若要写在路由中,如下 3.转到相应的路由(页面) 1.采用location服务 2.采用 transiti ...
- selenium操作下拉选和网页提示框
import time from selenium import webdriver from selenium.webdriver.support.select import Select#处理下拉 ...
- python语言特性简要记载
1.python是解释型语言,而c,c++等是编译型语言. 2.python是动态类型语言,这意味着你不需要在声明变量时指定类型. 3.Python是面向对象语言,所有允许定义类并且可以继承和组合.P ...
- 在Windows 10 操作系统打开Windows Mobile 设备中心,要么双击无反应,要么正在启动后过会就关闭了
在Windows 10 操作系统打开Windows Mobile 设备中心,要么双击无反应,要么正在启动后过会就关闭了 解决方法: 1.运行:输入services.msc进入服务 2.找到(前提你的P ...
- 【leetcode】LCP 3. Programmable Robot
题目如下: 力扣团队买了一个可编程机器人,机器人初始位置在原点(0, 0).小伙伴事先给机器人输入一串指令command,机器人就会无限循环这条指令的步骤进行移动.指令有两种: U: 向y轴正方向移动 ...
- linux-包管理器-4
安装 升级 查询 导入公钥 rpm -K|checksig rpmfile 检查包的完整性和签名 rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 ...
- maven项目使用自己创建的jar包--maven without test code
eclipse版本为2018-12(4.10.0) 1.创建一个jar包 首先自己建立了一个maven project,名为jweb.GAV坐标: <groupId>amberai< ...
- codevs 1079 回家x
1079 回家 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题目描述 Description 现在是晚餐时间,而母牛们在外面分散的牧场中. 农民约翰 ...
- mysql增删改查相关操作
mysql增删改查相关操作 以前用mysql用的少,对于数据库相关的操作不熟悉,现在开始要接触数据库了,记录一下相关的基础操作吧. 1.数据库的授权操作 # mysql -u root -p Ente ...