Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f0827cd

Browse files
Merge pull request SharingSource#481 from SharingSource/ac_oier
✨feat: Add 868 & Modify 2069
2 parents d2f0cb5 + 9028c46 commit f0827cd

File tree

3 files changed

+83
-1
lines changed

3 files changed

+83
-1
lines changed

‎Index/模拟.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
| [846. 一手顺子](https://leetcode-cn.com/problems/hand-of-straights/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/hand-of-straights/solution/gong-shui-san-xie-shu-ju-jie-gou-mo-ni-t-4hxw/) | 中等 | 🤩🤩🤩 |
9797
| [859. 亲密字符串](https://leetcode-cn.com/problems/buddy-strings/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/buddy-strings/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-m-q056/) | 简单 | 🤩🤩🤩🤩🤩 |
9898
| [867. 转置矩阵](https://leetcode-cn.com/problems/transpose-matrix/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/transpose-matrix/solution/yi-you-wei-jin-huo-xu-ni-neng-kan-kan-zh-m53m/) | 简单 | 🤩🤩🤩🤩 |
99+
| [868. 二进制间距](https://leetcode-cn.com/problems/binary-gap/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/binary-gap/solution/by-ac_oier-2sui/) | 简单 | 🤩🤩🤩🤩 |
99100
| [884. 两句话中的不常见单词](https://leetcode-cn.com/problems/uncommon-words-from-two-sentences/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/uncommon-words-from-two-sentences/solution/gong-shui-san-xie-shu-ju-jie-gou-mo-ni-t-wwam/) | 简单 | 🤩🤩🤩🤩 |
100101
| [896. 单调数列](https://leetcode-cn.com/problems/monotonic-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/monotonic-array/solution/wei-shi-yao-yi-ci-bian-li-yao-bi-liang-c-uglp/) | 简单 | 🤩🤩🤩🤩 |
101102
| [997. 找到小镇的法官](https://leetcode-cn.com/problems/find-the-town-judge/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-the-town-judge/solution/gong-shui-san-xie-jian-dan-chu-du-ru-du-5ms57/) | 简单 | 🤩🤩🤩🤩 |

‎LeetCode/2061-2070/2069. 模拟行走机器人 II(中等).md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class Robot {
115115

116116
### 最后
117117

118-
这是我们「刷穿 LeetCode」系列文章的第 `No.2169` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
118+
这是我们「刷穿 LeetCode」系列文章的第 `No.2069` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
119119

120120
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
121121

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[868. 二进制间距](https://leetcode-cn.com/problems/binary-gap/solution/by-ac_oier-2sui/)** ,难度为 **简单**
4+
5+
Tag : 「模拟」
6+
7+
8+
9+
给定一个正整数 $n,ドル找到并返回 $n$ 的二进制表示中两个 相邻 1ドル$ 之间的 最长距离 。如果不存在两个相邻的 1ドル,ドル返回 0ドル$ 。
10+
11+
如果只有 0ドル$ 将两个 1ドル$ 分隔开(可能不存在 0ドル$ ),则认为这两个 1ドル$ 彼此 相邻 。两个 1ドル$ 之间的距离是它们的二进制表示中位置的绝对差。例如,`"1001"` 中的两个 1ドル$ 的距离为 3ドル$ 。
12+
13+
示例 1:
14+
```
15+
输入:n = 22
16+
17+
输出:2
18+
19+
解释:22 的二进制是 "10110" 。
20+
在 22 的二进制表示中,有三个 1,组成两对相邻的 1 。
21+
第一对相邻的 1 中,两个 1 之间的距离为 2 。
22+
第二对相邻的 1 中,两个 1 之间的距离为 1 。
23+
答案取两个距离之中最大的,也就是 2 。
24+
```
25+
示例 2:
26+
```
27+
输入:n = 8
28+
29+
输出:0
30+
31+
解释:8 的二进制是 "1000" 。
32+
在 8 的二进制表示中没有相邻的两个 1,所以返回 0 。
33+
```
34+
示例 3:
35+
```
36+
输入:n = 5
37+
38+
输出:2
39+
40+
解释:5 的二进制是 "101" 。
41+
```
42+
43+
提示:
44+
* 1ドル <= n <= 10^9$
45+
46+
---
47+
48+
### 模拟
49+
50+
根据题意进行模拟即可,遍历 $n$ 的二进制中的每一位 $i,ドル同时记录上一位 1ドル$ 的位置 $j,ドル即可得到所有相邻 1ドル$ 的间距,所有间距取 $\max$ 即是答案。
51+
52+
代码:
53+
```Java
54+
class Solution {
55+
public int binaryGap(int n) {
56+
int ans = 0;
57+
for (int i = 31, j = -1; i >= 0; i--) {
58+
if (((n >> i) & 1) == 1) {
59+
if (j != -1) ans = Math.max(ans, j - i);
60+
j = i;
61+
}
62+
}
63+
return ans;
64+
}
65+
}
66+
```
67+
* 时间复杂度:$O(\log{n})$
68+
* 空间复杂度:$O(1)$
69+
70+
---
71+
72+
### 最后
73+
74+
这是我们「刷穿 LeetCode」系列文章的第 `No.868` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
75+
76+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
77+
78+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
79+
80+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
81+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /