Skip to content

Commit 6f2d37f

Browse files
authored
feat: add biweekly contest 184 (#5243)
1 parent cb07f54 commit 6f2d37f

19 files changed

Lines changed: 1254 additions & 2 deletions

File tree

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
---
2+
comments: true
3+
difficulty: 简单
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3900-3999/3950.Exactly%20One%20Consecutive%20Set%20Bits%20Pair/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3950. 恰好一对连续置位](https://leetcode.cn/problems/exactly-one-consecutive-set-bits-pair)
10+
11+
[English Version](/solution/3900-3999/3950.Exactly%20One%20Consecutive%20Set%20Bits%20Pair/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p>给你一个整数 <code>n</code> 。</p>
18+
19+
<p>如果其二进制表示中 <strong>恰好 </strong>仅包含 <strong>一对</strong> <strong>连续的置位</strong> ,则返回 <code>true</code> ,否则返回 <code>false</code> 。</p>
20+
整数中的 <strong>置位</strong> 是指其 <strong>二进制</strong> 表示中的 <code>1</code> 。
21+
22+
<p>&nbsp;</p>
23+
24+
<p><strong class="example">示例 1:</strong></p>
25+
26+
<div class="example-block">
27+
<p><strong>输入:</strong> <span class="example-io">nums = 6</span></p>
28+
29+
<p><strong>输出:</strong> <span class="example-io">true</span></p>
30+
31+
<p><strong>解释:</strong></p>
32+
33+
<ul>
34+
<li>6 的二进制表示为 <code>110</code> 。</li>
35+
<li>恰好存在一对连续的置位(<code>"11"</code>)。因此,答案为 <code>true</code> 。</li>
36+
</ul>
37+
</div>
38+
39+
<p><strong class="example">示例 2:</strong></p>
40+
41+
<div class="example-block">
42+
<p><strong>输入:</strong> <span class="example-io">nums = 5</span></p>
43+
44+
<p><strong>输出:</strong> <span class="example-io">false</span></p>
45+
46+
<p><strong>解释:</strong></p>
47+
48+
<ul>
49+
<li>5 的二进制表示为 <code>101</code> 。</li>
50+
<li>不存在连续的置位。因此,答案为 <code>false</code> 。</li>
51+
</ul>
52+
</div>
53+
54+
<p>&nbsp;</p>
55+
56+
<p><strong>提示:</strong></p>
57+
58+
<ul>
59+
<li><code>0 &lt;= n &lt;= 10<sup>5</sup></code></li>
60+
</ul>
61+
62+
<!-- description:end -->
63+
64+
## 解法
65+
66+
<!-- solution:start -->
67+
68+
### 方法一:模拟
69+
70+
我们用一个变量 $\textit{pre}$ 记录上一个位的数字,初始时 $\textit{pre} = 0$,用另一个变量 $\textit{vis}$ 记录是否已经找到一对连续置位,初始时 $\textit{vis} = \text{false}$。
71+
72+
遍历 $n$ 的每个二进制位,记当前二进制位为 $\textit{cur}$。如果 $\textit{pre} = \textit{cur} = 1$,此时如果 $\textit{vis} = \text{true}$,说明存在多对连续置位,直接返回 $\text{false}$,否则我们将 $\textit{vis}$ 置为 $\text{true}$。然后我们更新 $\textit{pre} = \textit{cur}$,继续遍历下个二进制位。
73+
74+
遍历结束后,如果 $\textit{vis} = \text{true}$,返回 $\text{true}$,否则返回 $\text{false}$。
75+
76+
时间复杂度 $O(\log n)$,空间复杂度 $O(1)$。
77+
78+
<!-- tabs:start -->
79+
80+
#### Python3
81+
82+
```python
83+
class Solution:
84+
def consecutiveSetBits(self, n: int) -> bool:
85+
pre = 0
86+
vis = False
87+
while n:
88+
cur = n & 1
89+
if pre == cur == 1:
90+
if vis:
91+
return False
92+
vis = True
93+
pre = cur
94+
n = n >> 1
95+
return vis
96+
```
97+
98+
#### Java
99+
100+
```java
101+
class Solution {
102+
public boolean consecutiveSetBits(int n) {
103+
boolean vis = false;
104+
for (int pre = 0; n > 0; n >>= 1) {
105+
int cur = n & 1;
106+
if (pre == cur && cur == 1) {
107+
if (vis) {
108+
return false;
109+
}
110+
vis = true;
111+
}
112+
pre = cur;
113+
}
114+
return vis;
115+
}
116+
}
117+
```
118+
119+
#### C++
120+
121+
```cpp
122+
class Solution {
123+
public:
124+
bool consecutiveSetBits(int n) {
125+
bool vis = false;
126+
for (int pre = 0; n > 0; n >>= 1) {
127+
int cur = n & 1;
128+
if (pre == cur && cur == 1) {
129+
if (vis) {
130+
return false;
131+
}
132+
vis = true;
133+
}
134+
pre = cur;
135+
}
136+
return vis;
137+
}
138+
};
139+
```
140+
141+
#### Go
142+
143+
```go
144+
func consecutiveSetBits(n int) bool {
145+
vis := false
146+
for pre := 0; n > 0; n >>= 1 {
147+
cur := n & 1
148+
if pre == cur && cur == 1 {
149+
if vis {
150+
return false
151+
}
152+
vis = true
153+
}
154+
pre = cur
155+
}
156+
return vis
157+
}
158+
```
159+
160+
#### TypeScript
161+
162+
```ts
163+
function consecutiveSetBits(n: number): boolean {
164+
let vis = false;
165+
for (let pre = 0; n > 0; n >>= 1) {
166+
const cur = n & 1;
167+
if (pre === cur && cur === 1) {
168+
if (vis) {
169+
return false;
170+
}
171+
vis = true;
172+
}
173+
pre = cur;
174+
}
175+
return vis;
176+
}
177+
```
178+
179+
<!-- tabs:end -->
180+
181+
<!-- solution:end -->
182+
183+
<!-- problem:end -->
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
---
2+
comments: true
3+
difficulty: Easy
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3900-3999/3950.Exactly%20One%20Consecutive%20Set%20Bits%20Pair/README_EN.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3950. Exactly One Consecutive Set Bits Pair](https://leetcode.com/problems/exactly-one-consecutive-set-bits-pair)
10+
11+
[中文文档](/solution/3900-3999/3950.Exactly%20One%20Consecutive%20Set%20Bits%20Pair/README.md)
12+
13+
## Description
14+
15+
<!-- description:start -->
16+
17+
<p>You are given an integer <code>n</code>.</p>
18+
19+
<p>Return <code>true</code> if its binary representation contains <strong>exactly one</strong> pair of <strong>consecutive</strong> <span data-keyword="set-bit">set bits</span>, and <code>false</code> otherwise.</p>
20+
21+
<p>&nbsp;</p>
22+
<p><strong class="example">Example 1:</strong></p>
23+
24+
<div class="example-block">
25+
<p><strong>Input:</strong> <span class="example-io">nums = 6</span></p>
26+
27+
<p><strong>Output:</strong> <span class="example-io">true</span></p>
28+
29+
<p><strong>Explanation:</strong></p>
30+
31+
<ul>
32+
<li>Binary representation of 6 is <code>110</code>.</li>
33+
<li>There is exactly one pair of consecutive set bits (<code>&quot;11&quot;</code>). Thus, the answer is <code>true</code>​​​​​​​.</li>
34+
</ul>
35+
</div>
36+
37+
<p><strong class="example">Example 2:</strong></p>
38+
39+
<div class="example-block">
40+
<p><strong>Input:</strong> <span class="example-io">nums = 5</span></p>
41+
42+
<p><strong>Output:</strong> <span class="example-io">false</span></p>
43+
44+
<p><strong>Explanation:</strong></p>
45+
46+
<ul>
47+
<li>Binary representation of 5 is <code>101</code>.</li>
48+
<li>There are no consecutive set bits. Thus, the answer is <code>false</code>​​​​​​​.</li>
49+
</ul>
50+
</div>
51+
52+
<p>&nbsp;</p>
53+
<p><strong>Constraints:</strong></p>
54+
55+
<ul>
56+
<li><code>0 &lt;= n &lt;= 10<sup>5</sup></code></li>
57+
</ul>
58+
59+
<!-- description:end -->
60+
61+
## Solutions
62+
63+
<!-- solution:start -->
64+
65+
### Solution 1: Simulation
66+
67+
We use a variable $\textit{pre}$ to record the digit of the previous bit, initialized to $\textit{pre} = 0$, and another variable $\textit{vis}$ to record whether a pair of consecutive set bits has already been found, initialized to $\textit{vis} = \text{false}$.
68+
69+
Iterate through each binary bit of $n$, and denote the current binary bit as $\textit{cur}$. If $\textit{pre} = \textit{cur} = 1$, and if $\textit{vis} = \text{true}$ at this moment, it indicates that there are multiple pairs of consecutive set bits, so we directly return $\text{false}$. Otherwise, we set $\textit{vis}$ to $\text{true}$. Then, we update $\textit{pre} = \textit{cur}$ and continue to iterate through the next binary bit.
70+
71+
After the iteration ends, if $\textit{vis} = \text{true}$, return $\text{true}$; otherwise, return $\text{false}$.
72+
73+
The time complexity is $O(\log n)$, and the space complexity is $O(1)$.
74+
75+
<!-- tabs:start -->
76+
77+
#### Python3
78+
79+
```python
80+
class Solution:
81+
def consecutiveSetBits(self, n: int) -> bool:
82+
pre = 0
83+
vis = False
84+
while n:
85+
cur = n & 1
86+
if pre == cur == 1:
87+
if vis:
88+
return False
89+
vis = True
90+
pre = cur
91+
n = n >> 1
92+
return vis
93+
```
94+
95+
#### Java
96+
97+
```java
98+
class Solution {
99+
public boolean consecutiveSetBits(int n) {
100+
boolean vis = false;
101+
for (int pre = 0; n > 0; n >>= 1) {
102+
int cur = n & 1;
103+
if (pre == cur && cur == 1) {
104+
if (vis) {
105+
return false;
106+
}
107+
vis = true;
108+
}
109+
pre = cur;
110+
}
111+
return vis;
112+
}
113+
}
114+
```
115+
116+
#### C++
117+
118+
```cpp
119+
class Solution {
120+
public:
121+
bool consecutiveSetBits(int n) {
122+
bool vis = false;
123+
for (int pre = 0; n > 0; n >>= 1) {
124+
int cur = n & 1;
125+
if (pre == cur && cur == 1) {
126+
if (vis) {
127+
return false;
128+
}
129+
vis = true;
130+
}
131+
pre = cur;
132+
}
133+
return vis;
134+
}
135+
};
136+
```
137+
138+
#### Go
139+
140+
```go
141+
func consecutiveSetBits(n int) bool {
142+
vis := false
143+
for pre := 0; n > 0; n >>= 1 {
144+
cur := n & 1
145+
if pre == cur && cur == 1 {
146+
if vis {
147+
return false
148+
}
149+
vis = true
150+
}
151+
pre = cur
152+
}
153+
return vis
154+
}
155+
```
156+
157+
#### TypeScript
158+
159+
```ts
160+
function consecutiveSetBits(n: number): boolean {
161+
let vis = false;
162+
for (let pre = 0; n > 0; n >>= 1) {
163+
const cur = n & 1;
164+
if (pre === cur && cur === 1) {
165+
if (vis) {
166+
return false;
167+
}
168+
vis = true;
169+
}
170+
pre = cur;
171+
}
172+
return vis;
173+
}
174+
```
175+
176+
<!-- tabs:end -->
177+
178+
<!-- solution:end -->
179+
180+
<!-- problem:end -->
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
bool consecutiveSetBits(int n) {
4+
bool vis = false;
5+
for (int pre = 0; n > 0; n >>= 1) {
6+
int cur = n & 1;
7+
if (pre == cur && cur == 1) {
8+
if (vis) {
9+
return false;
10+
}
11+
vis = true;
12+
}
13+
pre = cur;
14+
}
15+
return vis;
16+
}
17+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func consecutiveSetBits(n int) bool {
2+
vis := false
3+
for pre := 0; n > 0; n >>= 1 {
4+
cur := n & 1
5+
if pre == cur && cur == 1 {
6+
if vis {
7+
return false
8+
}
9+
vis = true
10+
}
11+
pre = cur
12+
}
13+
return vis
14+
}

0 commit comments

Comments
 (0)