|
1 | 1 | [#0868-binary-gap] |
2 | | -= 868. Binary Gap |
| 2 | += 868. 二进制间距 |
3 | 3 |
|
4 | | -{leetcode}/problems/binary-gap/[LeetCode - Binary Gap^] |
| 4 | +https://leetcode.cn/problems/binary-gap/[LeetCode - 868. 二进制间距^] |
5 | 5 |
|
6 | | -Given a positive integer `N`, find and return the longest distance between two consecutive 1's in the binary representation of `N`. |
| 6 | +给定一个正整数 `n`,找到并返回 `n` 的二进制表示中两个 *相邻* 1 之间的 *最长距离* 。如果不存在两个相邻的 1,返回 `0` 。 |
7 | 7 |
|
8 | | -If there aren't two consecutive 1's, return <font face="monospace">0. |
| 8 | +如果只有 `0` 将两个 `1` 分隔开(可能不存在 `0` ),则认为这两个 1 彼此 *相邻* 。两个 `1` 之间的距离是它们的二进制表示中位置的绝对差。例如,`1001` 中的两个 `1` 的距离为 3。 |
9 | 9 |
|
10 | | - |
| 10 | +*示例 1:* |
11 | 11 |
|
| 12 | +.... |
| 13 | +输入:n = 22 |
| 14 | +输出:2 |
| 15 | +解释:22 的二进制是 "10110" 。 |
| 16 | +在 22 的二进制表示中,有三个 1,组成两对相邻的 1 。 |
| 17 | +第一对相邻的 1 中,两个 1 之间的距离为 2 。 |
| 18 | +第二对相邻的 1 中,两个 1 之间的距离为 1 。 |
| 19 | +答案取两个距离之中最大的,也就是 2 。 |
| 20 | +.... |
12 | 21 |
|
| 22 | +*示例 2:* |
13 | 23 |
|
| 24 | +.... |
| 25 | +输入:n = 8 |
| 26 | +输出:0 |
| 27 | +解释:8 的二进制是 "1000" 。 |
| 28 | +在 8 的二进制表示中没有相邻的两个 1,所以返回 0 。 |
| 29 | +.... |
14 | 30 |
|
| 31 | +*示例 3:* |
15 | 32 |
|
| 33 | +.... |
| 34 | +输入:n = 5 |
| 35 | +输出:2 |
| 36 | +解释:5 的二进制是 "101" 。 |
| 37 | +.... |
16 | 38 |
|
| 39 | +*提示:* |
17 | 40 |
|
| 41 | +* `1 \<= n \<= 10^9^` |
18 | 42 |
|
19 | 43 |
|
20 | 44 |
|
| 45 | +== 思路分析 |
21 | 46 |
|
22 | | -*Example 1:* |
23 | | - |
24 | | -[subs="verbatim,quotes,macros"] |
25 | | ----- |
26 | | -*Input:* 22 |
27 | | -*Output:* 2 |
28 | | -*Explanation:* |
29 | | -22 in binary is 0b10110. |
30 | | -In the binary representation of 22, there are three ones, and two consecutive pairs of 1's. |
31 | | -The first consecutive pair of 1's have distance 2. |
32 | | -The second consecutive pair of 1's have distance 1. |
33 | | -The answer is the largest of these two distances, which is 2. |
34 | | ----- |
35 | | - |
36 | | - |
37 | | -*Example 2:* |
38 | | - |
39 | | -[subs="verbatim,quotes,macros"] |
40 | | ----- |
41 | | -*Input:* 5 |
42 | | -*Output:* 2 |
43 | | -*Explanation:* |
44 | | -5 in binary is 0b101. |
45 | | ----- |
46 | | - |
47 | | - |
48 | | -*Example 3:* |
49 | | - |
50 | | -[subs="verbatim,quotes,macros"] |
51 | | ----- |
52 | | -*Input:* 6 |
53 | | -*Output:* 1 |
54 | | -*Explanation:* |
55 | | -6 in binary is 0b110. |
56 | | ----- |
57 | | - |
58 | | - |
59 | | -*Example 4:* |
60 | | - |
61 | | -[subs="verbatim,quotes,macros"] |
62 | | ----- |
63 | | -*Input:* 8 |
64 | | -*Output:* 0 |
65 | | -*Explanation:* |
66 | | -8 in binary is 0b1000. |
67 | | -There aren't any consecutive pairs of 1's in the binary representation of 8, so we return 0. |
68 | | ----- |
69 | | - |
70 | | - |
71 | | - |
72 | | - |
73 | | - |
74 | | - |
75 | | -*Note:* |
76 | | - |
77 | | - |
78 | | -* `1 <= N <= 10^9` |
79 | | - |
80 | | - |
81 | | - |
82 | | - |
83 | | - |
84 | | - |
85 | | - |
86 | | - |
87 | | - |
88 | | - |
| 47 | +双指针:记录上一个 `1`,寻找下一个 `1`,然后计算两者的距离。 |
89 | 48 |
|
90 | 49 | [[src-0868]] |
| 50 | +[tabs] |
| 51 | +==== |
| 52 | +一刷:: |
| 53 | ++ |
| 54 | +-- |
91 | 55 | [{java_src_attr}] |
92 | 56 | ---- |
93 | 57 | include::{sourcedir}/_0868_BinaryGap.java[tag=answer] |
94 | 58 | ---- |
| 59 | +-- |
| 60 | +
|
| 61 | +// 二刷:: |
| 62 | +// + |
| 63 | +// -- |
| 64 | +// [{java_src_attr}] |
| 65 | +// ---- |
| 66 | +// include::{sourcedir}/_0868_BinaryGap_2.java[tag=answer] |
| 67 | +// ---- |
| 68 | +// -- |
| 69 | +==== |
| 70 | + |
| 71 | + |
| 72 | +== 参考资料 |
95 | 73 |
|
| 74 | +. https://leetcode.cn/problems/binary-gap/solutions/3906088/ji-suan-wei-ling-ge-shu-pythonjavacgocjs-jwgs/[868. 二进制间距 - 计算尾零个数^] |
| 75 | +. https://leetcode.cn/problems/binary-gap/solutions/1441893/er-jin-zhi-jian-ju-by-leetcode-solution-dh2q/[868. 二进制间距 - 官方题解^] |
0 commit comments