|
…
|
||
|---|---|---|
| .. | ||
| README.md | ||
| README_EN.md | ||
| Solution.cpp | ||
| Solution.go | ||
| Solution.java | ||
| Solution.py | ||
| Solution.rs | ||
| Solution.ts | ||
README_EN.md
| comments | difficulty | edit_url | tags | |
|---|---|---|---|---|
| true | Easy | https://github.com/doocs/leetcode/edit/main/solution/0800-0899/0868.Binary%20Gap/README_EN.md |
|
868. Binary Gap
Description
Given a positive integer n, find and return the longest distance between any two adjacent 1's in the binary representation of n. If there are no two adjacent 1's, return 0.
Two 1's are adjacent if there are only 0's separating them (possibly no 0's). The distance between two 1's is the absolute difference between their bit positions. For example, the two 1's in "1001" have a distance of 3.
Example 1:
Input: n = 22 Output: 2 Explanation: 22 in binary is "10110". The first adjacent pair of 1's is "10110" with a distance of 2. The second adjacent pair of 1's is "10110" with a distance of 1. The answer is the largest of these two distances, which is 2. Note that "10110" is not a valid pair since there is a 1 separating the two 1's underlined.
Example 2:
Input: n = 8 Output: 0 Explanation: 8 in binary is "1000". There are not any adjacent pairs of 1's in the binary representation of 8, so we return 0.
Example 3:
Input: n = 5 Output: 2 Explanation: 5 in binary is "101".
Constraints:
1 <= n <= 109
Solutions
Solution 1: Bit Manipulation
We use two pointers \textit{pre} and \textit{cur} to represent the positions of the previous and current 1 bits, respectively. Initially, \textit{pre} = 100 and \textit{cur} = 0. Then, we traverse the binary representation of n. When we encounter a 1, we calculate the distance between the current position and the previous 1 position and update the answer.
The time complexity is O(\log n), where n is the given integer. The space complexity is O(1).
Python3
class Solution:
def binaryGap(self, n: int) -> int:
ans = 0
pre, cur = inf, 0
while n:
if n & 1:
ans = max(ans, cur - pre)
pre = cur
cur += 1
n >>= 1
return ans
Java
class Solution {
public int binaryGap(int n) {
int ans = 0;
for (int pre = 100, cur = 0; n != 0; n >>= 1) {
if (n % 2 == 1) {
ans = Math.max(ans, cur - pre);
pre = cur;
}
++cur;
}
return ans;
}
}
C++
class Solution {
public:
int binaryGap(int n) {
int ans = 0;
for (int pre = 100, cur = 0; n != 0; n >>= 1) {
if (n & 1) {
ans = max(ans, cur - pre);
pre = cur;
}
++cur;
}
return ans;
}
};
Go
func binaryGap(n int) (ans int) {
for pre, cur := 100, 0; n != 0; n >>= 1 {
if n&1 == 1 {
ans = max(ans, cur-pre)
pre = cur
}
cur++
}
return
}
TypeScript
function binaryGap(n: number): number {
let ans = 0;
for (let pre = 100, cur = 0; n; n >>= 1) {
if (n & 1) {
ans = Math.max(ans, cur - pre);
pre = cur;
}
++cur;
}
return ans;
}
Rust
impl Solution {
pub fn binary_gap(mut n: i32) -> i32 {
let mut ans = 0;
let mut pre = 100;
let mut cur = 0;
while n != 0 {
if n % 2 == 1 {
ans = ans.max(cur - pre);
pre = cur;
}
cur += 1;
n >>= 1;
}
ans
}
}