当前位置:网站首页>Force deduction solution summary 868- binary spacing
Force deduction solution summary 868- binary spacing
2022-06-12 02:08:00 【Lost summer】
Directory links :
Force buckle programming problem - The solution sums up _ Share + Record -CSDN Blog
GitHub Synchronous question brushing items :
https://github.com/September26/java-algorithms
Original link : Power button
describe :
Given a positive integer n, Find and return to n In the binary representation of adjacent 1 Between Longest distance . If there are no two adjacent 1, return 0 .
If only 0 Put two 1 Separate ( May not exist 0 ), We think these two 1 each other adjacent . Two 1 The distance between them is the absolute difference of their positions in the binary representation . for example ,"1001" Two of them 1 The distance to 3 .
Example 1:
Input :n = 22
Output :2
explain :22 The binary of is "10110" .
stay 22 In the binary representation of , There are three 1, Make up two adjacent pairs of 1 .
The first pair of adjacent 1 in , Two 1 The distance between is 2 .
The second pair is adjacent 1 in , Two 1 The distance between is 1 .
The answer is the largest of the two distances , That is to say 2 .
Example 2:
Input :n = 8
Output :0
explain :8 The binary of is "1000" .
stay 8 There are no adjacent two in the binary representation of 1, So back 0 .
Example 3:
Input :n = 5
Output :2
explain :5 The binary of is "101" .
Tips :
1 <= n <= 109
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/binary-gap
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Their thinking :
* Their thinking : * To string, Then judge the distance
Code :
public class Solution868 {
public int binaryGap(int n) {
char[] chars = Integer.toString(n, 2).toCharArray();
int result = 0;
int currentIndex = 0;
for (int i = 0; i < chars.length; i++) {
char aChar = chars[i];
if (aChar == '0') {
continue;
}
result = Math.max(i - currentIndex, result);
currentIndex = i;
}
return result;
}
}边栏推荐
- 力扣解法汇总883-三维形体投影面积
- 为什么我们要使用谷歌搜索广告?
- Ozzanation - système d'action basé sur sse
- Smartbi helps you solve the problem of losing high-value customers
- MySQL高级部分知识点
- Xcall cluster script (view JPS command)
- 消防栓监测系统毕业设计---论文(附加最全面的从硬件电路设计->驱动程序设计->阿里云物联网搭建->安卓APP设计)
- Bracket generation (backtracking)
- 力扣解法汇总462-最少移动次数使数组元素相等 II
- Design principle [Demeter's Law]
猜你喜欢

MySQL table common operation mind map

Various error reporting solutions encountered by Kali during Empire installation

RPA introduction

决定广告质量的三个主要因素

Almost all schools will ask for the second round exam! Come in and recite the answer!

How to automatically color cells in Excel

Linux(CentOS6)安装MySQL5.5版本数据库

el-upload上传文件

Knowledge points of mall development

Add sequence number column to MySQL query result set
随机推荐
力扣解法汇总1037-有效的回旋镖
In Net platform using reflectiondynamicobject to optimize reflection calling code
Invert words in a string (split, double ended queue)
android html5页面加载缓存优化
SwiftyJSON解析本地JSON文件
ACL 2022 | 预训练语言模型和图文模型的强强联合
打包一个包含手表端应用的手机端APK应用—Ticwear
The release of star ring kundb 2.2 provides a new choice for business systems with high concurrent transactions and queries
Knowledge points of mall development
PHP builds a high-performance API architecture based on sw-x framework (III)
MySQL表常用操作思维导图
力扣解法汇总358-迷你语法分析器
力扣解法汇总713- 乘积小于 K 的子数组
【无标题】2022煤矿安全检查考题及在线模拟考试
Linux(CentOS7)安裝MySQL-5.7版本
Implementation scheme of iteration and combination pattern for general tree structure
力扣解法汇总450-删除二叉搜索树中的节点
UE4\UE5触摸屏touch事件:单指、双指
力扣解法汇总953-验证外星语词典
Add sequence number column to MySQL query result set