Skip to content

Commit 548dd9d

Browse files
authored
feat: add weekly contest 426 (#3829)
1 parent c9842c3 commit 548dd9d

File tree

32 files changed

+1907
-1
lines changed

32 files changed

+1907
-1
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
---
2+
comments: true
3+
difficulty: 简单
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3370.Smallest%20Number%20With%20All%20Set%20Bits/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3370. 仅含置位位的最小整数](https://leetcode.cn/problems/smallest-number-with-all-set-bits)
10+
11+
[English Version](/solution/3300-3399/3370.Smallest%20Number%20With%20All%20Set%20Bits/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p>给你一个正整数 <code>n</code>。</p>
18+
19+
<p>返回&nbsp;<strong>大于等于</strong> <code>n</code>&nbsp;且二进制表示仅包含&nbsp;<strong>置位&nbsp;</strong>位的&nbsp;<strong>最小&nbsp;</strong>整数 <code>x</code>&nbsp;。</p>
20+
21+
<p><strong>置位&nbsp;</strong>位指的是二进制表示中值为 <code>1</code> 的位。</p>
22+
23+
<p>&nbsp;</p>
24+
25+
<p><strong class="example">示例 1:</strong></p>
26+
27+
<div class="example-block">
28+
<p><strong>输入:</strong> <span class="example-io">n = 5</span></p>
29+
30+
<p><strong>输出:</strong> <span class="example-io">7</span></p>
31+
32+
<p><strong>解释:</strong></p>
33+
34+
<p>7 的二进制表示是 <code>"111"</code>。</p>
35+
</div>
36+
37+
<p><strong class="example">示例 2:</strong></p>
38+
39+
<div class="example-block">
40+
<p><strong>输入:</strong> <span class="example-io">n = 10</span></p>
41+
42+
<p><strong>输出:</strong> <span class="example-io">15</span></p>
43+
44+
<p><strong>解释:</strong></p>
45+
46+
<p>15 的二进制表示是 <code>"1111"</code>。</p>
47+
</div>
48+
49+
<p><strong class="example">示例 3:</strong></p>
50+
51+
<div class="example-block">
52+
<p><strong>输入:</strong> <span class="example-io">n = 3</span></p>
53+
54+
<p><strong>输出:</strong> <span class="example-io">3</span></p>
55+
56+
<p><strong>解释:</strong></p>
57+
58+
<p>3 的二进制表示是 <code>"11"</code>。</p>
59+
</div>
60+
61+
<p>&nbsp;</p>
62+
63+
<p><strong>提示:</strong></p>
64+
65+
<ul>
66+
<li><code>1 &lt;= n &lt;= 1000</code></li>
67+
</ul>
68+
69+
<!-- description:end -->
70+
71+
## 解法
72+
73+
<!-- solution:start -->
74+
75+
### 方法一
76+
77+
<!-- tabs:start -->
78+
79+
#### Python3
80+
81+
```python
82+
class Solution:
83+
def smallestNumber(self, n: int) -> int:
84+
x = 1
85+
while x - 1 < n:
86+
x <<= 1
87+
return x - 1
88+
```
89+
90+
#### Java
91+
92+
```java
93+
class Solution {
94+
public int smallestNumber(int n) {
95+
int x = 1;
96+
while (x - 1 < n) {
97+
x <<= 1;
98+
}
99+
return x - 1;
100+
}
101+
}
102+
```
103+
104+
#### C++
105+
106+
```cpp
107+
class Solution {
108+
public:
109+
int smallestNumber(int n) {
110+
int x = 1;
111+
while (x - 1 < n) {
112+
x <<= 1;
113+
}
114+
return x - 1;
115+
}
116+
};
117+
```
118+
119+
#### Go
120+
121+
```go
122+
func smallestNumber(n int) int {
123+
x := 1
124+
for x-1 < n {
125+
x <<= 1
126+
}
127+
return x - 1
128+
}
129+
```
130+
131+
#### TypeScript
132+
133+
```ts
134+
function smallestNumber(n: number): number {
135+
let x = 1;
136+
while (x - 1 < n) {
137+
x <<= 1;
138+
}
139+
return x - 1;
140+
}
141+
```
142+
143+
<!-- tabs:end -->
144+
145+
<!-- solution:end -->
146+
147+
<!-- problem:end -->
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
---
2+
comments: true
3+
difficulty: Easy
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3370.Smallest%20Number%20With%20All%20Set%20Bits/README_EN.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3370. Smallest Number With All Set Bits](https://leetcode.com/problems/smallest-number-with-all-set-bits)
10+
11+
[中文文档](/solution/3300-3399/3370.Smallest%20Number%20With%20All%20Set%20Bits/README.md)
12+
13+
## Description
14+
15+
<!-- description:start -->
16+
17+
<p>You are given a <em>positive</em> number <code>n</code>.</p>
18+
19+
<p>Return the <strong>smallest</strong> number <code>x</code> <strong>greater than</strong> or <strong>equal to</strong> <code>n</code>, such that the binary representation of <code>x</code> contains only <strong>set</strong> bits.</p>
20+
21+
<p>A <strong>set</strong> bit refers to a bit in the binary representation of a number that has a value of <code>1</code>.</p>
22+
23+
<p>&nbsp;</p>
24+
<p><strong class="example">Example 1:</strong></p>
25+
26+
<div class="example-block">
27+
<p><strong>Input:</strong> <span class="example-io">n = 5</span></p>
28+
29+
<p><strong>Output:</strong> <span class="example-io">7</span></p>
30+
31+
<p><strong>Explanation:</strong></p>
32+
33+
<p>The binary representation of 7 is <code>&quot;111&quot;</code>.</p>
34+
</div>
35+
36+
<p><strong class="example">Example 2:</strong></p>
37+
38+
<div class="example-block">
39+
<p><strong>Input:</strong> <span class="example-io">n = 10</span></p>
40+
41+
<p><strong>Output:</strong> <span class="example-io">15</span></p>
42+
43+
<p><strong>Explanation:</strong></p>
44+
45+
<p>The binary representation of 15 is <code>&quot;1111&quot;</code>.</p>
46+
</div>
47+
48+
<p><strong class="example">Example 3:</strong></p>
49+
50+
<div class="example-block">
51+
<p><strong>Input:</strong> <span class="example-io">n = 3</span></p>
52+
53+
<p><strong>Output:</strong> <span class="example-io">3</span></p>
54+
55+
<p><strong>Explanation:</strong></p>
56+
57+
<p>The binary representation of 3 is <code>&quot;11&quot;</code>.</p>
58+
</div>
59+
60+
<p>&nbsp;</p>
61+
<p><strong>Constraints:</strong></p>
62+
63+
<ul>
64+
<li><code>1 &lt;= n &lt;= 1000</code></li>
65+
</ul>
66+
67+
<!-- description:end -->
68+
69+
## Solutions
70+
71+
<!-- solution:start -->
72+
73+
### Solution 1
74+
75+
<!-- tabs:start -->
76+
77+
#### Python3
78+
79+
```python
80+
class Solution:
81+
def smallestNumber(self, n: int) -> int:
82+
x = 1
83+
while x - 1 < n:
84+
x <<= 1
85+
return x - 1
86+
```
87+
88+
#### Java
89+
90+
```java
91+
class Solution {
92+
public int smallestNumber(int n) {
93+
int x = 1;
94+
while (x - 1 < n) {
95+
x <<= 1;
96+
}
97+
return x - 1;
98+
}
99+
}
100+
```
101+
102+
#### C++
103+
104+
```cpp
105+
class Solution {
106+
public:
107+
int smallestNumber(int n) {
108+
int x = 1;
109+
while (x - 1 < n) {
110+
x <<= 1;
111+
}
112+
return x - 1;
113+
}
114+
};
115+
```
116+
117+
#### Go
118+
119+
```go
120+
func smallestNumber(n int) int {
121+
x := 1
122+
for x-1 < n {
123+
x <<= 1
124+
}
125+
return x - 1
126+
}
127+
```
128+
129+
#### TypeScript
130+
131+
```ts
132+
function smallestNumber(n: number): number {
133+
let x = 1;
134+
while (x - 1 < n) {
135+
x <<= 1;
136+
}
137+
return x - 1;
138+
}
139+
```
140+
141+
<!-- tabs:end -->
142+
143+
<!-- solution:end -->
144+
145+
<!-- problem:end -->
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public:
3+
int smallestNumber(int n) {
4+
int x = 1;
5+
while (x - 1 < n) {
6+
x <<= 1;
7+
}
8+
return x - 1;
9+
}
10+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
func smallestNumber(n int) int {
2+
x := 1
3+
for x-1 < n {
4+
x <<= 1
5+
}
6+
return x - 1
7+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution {
2+
public int smallestNumber(int n) {
3+
int x = 1;
4+
while (x - 1 < n) {
5+
x <<= 1;
6+
}
7+
return x - 1;
8+
}
9+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution:
2+
def smallestNumber(self, n: int) -> int:
3+
x = 1
4+
while x - 1 < n:
5+
x <<= 1
6+
return x - 1
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function smallestNumber(n: number): number {
2+
let x = 1;
3+
while (x - 1 < n) {
4+
x <<= 1;
5+
}
6+
return x - 1;
7+
}

0 commit comments

Comments
 (0)