当前位置:网站首页>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;
}
}边栏推荐
- 力扣解法汇总433-最小基因变化
- el-upload上传文件
- MySQL表常用操作思维导图
- Basedexclassloader
- Pagination writing of PHP security open 10 article list module
- 力扣解法汇总944-删列造序
- Leetcode 1005 maximized array sum after K negations
- Invert words in a string (split, double ended queue)
- [learn FPGA programming from scratch -20]: quick start chapter - operation steps 4-2-quick use of Altera quartz II tool (Modelsim co simulation, program download to altera development board)
- How can low code platforms improve cost effectiveness?
猜你喜欢

Graphical data analysis | data analysis tool map

Knowledge points of mall development

C language programming classic games - minesweeping

Graphic data analysis | data cleaning and pretreatment

Fatal error in launcher: unable to create process using

php开发 博客系统的公告模块的建立和引入

How WPS inserts a directory and the operating steps for quickly inserting a directory

The most comprehensive redis transaction control in 2022 (with illustration)

el-upload上传文件

Bracket generation (backtracking)
随机推荐
Summary of concrete (ground + wall) + Mountain crack data set (classification and target detection)
How to improve the advertising rating of advertising, that is, the quality score?
力扣解法汇总942-增减字符串匹配
决定广告质量的三个主要因素
Don't miss it! Five large data visualization screens that HR must collect
php开发 博客系统的公告模块的建立和引入
力扣解法汇总868-二进制间距
Leetcode 55 jump game
Summary of force deduction method 417- Pacific Atlantic current problems
消防栓监测系统毕业设计---论文(附加最全面的从硬件电路设计->驱动程序设计->阿里云物联网搭建->安卓APP设计)
Make ads more relevant by searching for additional information about ads
The establishment and introduction of the announcement module of PHP development blog system
Leetcode 1005 maximized array sum after K negations
如何最大化的利用各种匹配方式? ——Google SEM
Is there a female Bluetooth headset suitable for girls? 38 Bluetooth headsets worth getting started
A mystery of the end of vagrant up
el-upload上传文件
力扣解法汇总473-火柴拼正方形
PHP security development 13 column module of blog system
PHP development 09 article module deletion and article classification writing