868. Binary Gap Easy

@problem@discussion
#Bit Manipulation



1/**
2 * [868] Binary Gap
3 *
4 * 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.
5 * 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.
6 *  
7 * Example 1:
8 * 
9 * Input: n = 22
10 * Output: 2
11 * Explanation: 22 in binary is "10110".
12 * The first adjacent pair of 1's is "<u>1</u>0<u>1</u>10" with a distance of 2.
13 * The second adjacent pair of 1's is "10<u>11</u>0" with a distance of 1.
14 * The answer is the largest of these two distances, which is 2.
15 * Note that "<u>1</u>01<u>1</u>0" is not a valid pair since there is a 1 separating the two 1's underlined.
16 * 
17 * Example 2:
18 * 
19 * Input: n = 8
20 * Output: 0
21 * Explanation: 8 in binary is "1000".
22 * There are not any adjacent pairs of 1's in the binary representation of 8, so we return 0.
23 * 
24 * Example 3:
25 * 
26 * Input: n = 5
27 * Output: 2
28 * Explanation: 5 in binary is "101".
29 * 
30 *  
31 * Constraints:
32 * 
33 * 	1 <= n <= 10^9
34 * 
35 */
36pub struct Solution {}
37
38// problem: https://leetcode.com/problems/binary-gap/
39// discuss: https://leetcode.com/problems/binary-gap/discuss/?currentPage=1&orderBy=most_votes&query=
40
41// submission codes start here
42
43impl Solution {
44    pub fn binary_gap(n: i32) -> i32 {
45        0
46    }
47}
48
49// submission codes end
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54
55    #[test]
56    fn test_868() {
57    }
58}
59


Back
© 2025 bowen.ge All Rights Reserved.