Skip to content

feat: add weekly contest 426 #3829

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 147 additions & 0 deletions solution/3300-3399/3370.Smallest Number With All Set Bits/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
---
comments: true
difficulty: 简单
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3370.Smallest%20Number%20With%20All%20Set%20Bits/README.md
---

<!-- problem:start -->

# [3370. 仅含置位位的最小整数](https://leetcode.cn/problems/smallest-number-with-all-set-bits)

[English Version](/solution/3300-3399/3370.Smallest%20Number%20With%20All%20Set%20Bits/README_EN.md)

## 题目描述

<!-- description:start -->

<p>给你一个正整数 <code>n</code>。</p>

<p>返回&nbsp;<strong>大于等于</strong> <code>n</code>&nbsp;且二进制表示仅包含&nbsp;<strong>置位&nbsp;</strong>位的&nbsp;<strong>最小&nbsp;</strong>整数 <code>x</code>&nbsp;。</p>

<p><strong>置位&nbsp;</strong>位指的是二进制表示中值为 <code>1</code> 的位。</p>

<p>&nbsp;</p>

<p><strong class="example">示例 1:</strong></p>

<div class="example-block">
<p><strong>输入:</strong> <span class="example-io">n = 5</span></p>

<p><strong>输出:</strong> <span class="example-io">7</span></p>

<p><strong>解释:</strong></p>

<p>7 的二进制表示是 <code>"111"</code>。</p>
</div>

<p><strong class="example">示例 2:</strong></p>

<div class="example-block">
<p><strong>输入:</strong> <span class="example-io">n = 10</span></p>

<p><strong>输出:</strong> <span class="example-io">15</span></p>

<p><strong>解释:</strong></p>

<p>15 的二进制表示是 <code>"1111"</code>。</p>
</div>

<p><strong class="example">示例 3:</strong></p>

<div class="example-block">
<p><strong>输入:</strong> <span class="example-io">n = 3</span></p>

<p><strong>输出:</strong> <span class="example-io">3</span></p>

<p><strong>解释:</strong></p>

<p>3 的二进制表示是 <code>"11"</code>。</p>
</div>

<p>&nbsp;</p>

<p><strong>提示:</strong></p>

<ul>
<li><code>1 &lt;= n &lt;= 1000</code></li>
</ul>

<!-- description:end -->

## 解法

<!-- solution:start -->

### 方法一

<!-- tabs:start -->

#### Python3

```python
class Solution:
def smallestNumber(self, n: int) -> int:
x = 1
while x - 1 < n:
x <<= 1
return x - 1
```

#### Java

```java
class Solution {
public int smallestNumber(int n) {
int x = 1;
while (x - 1 < n) {
x <<= 1;
}
return x - 1;
}
}
```

#### C++

```cpp
class Solution {
public:
int smallestNumber(int n) {
int x = 1;
while (x - 1 < n) {
x <<= 1;
}
return x - 1;
}
};
```

#### Go

```go
func smallestNumber(n int) int {
x := 1
for x-1 < n {
x <<= 1
}
return x - 1
}
```

#### TypeScript

```ts
function smallestNumber(n: number): number {
let x = 1;
while (x - 1 < n) {
x <<= 1;
}
return x - 1;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
145 changes: 145 additions & 0 deletions solution/3300-3399/3370.Smallest Number With All Set Bits/README_EN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
---
comments: true
difficulty: Easy
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3370.Smallest%20Number%20With%20All%20Set%20Bits/README_EN.md
---

<!-- problem:start -->

# [3370. Smallest Number With All Set Bits](https://leetcode.com/problems/smallest-number-with-all-set-bits)

[中文文档](/solution/3300-3399/3370.Smallest%20Number%20With%20All%20Set%20Bits/README.md)

## Description

<!-- description:start -->

<p>You are given a <em>positive</em> number <code>n</code>.</p>

<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>

<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>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 5</span></p>

<p><strong>Output:</strong> <span class="example-io">7</span></p>

<p><strong>Explanation:</strong></p>

<p>The binary representation of 7 is <code>&quot;111&quot;</code>.</p>
</div>

<p><strong class="example">Example 2:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 10</span></p>

<p><strong>Output:</strong> <span class="example-io">15</span></p>

<p><strong>Explanation:</strong></p>

<p>The binary representation of 15 is <code>&quot;1111&quot;</code>.</p>
</div>

<p><strong class="example">Example 3:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 3</span></p>

<p><strong>Output:</strong> <span class="example-io">3</span></p>

<p><strong>Explanation:</strong></p>

<p>The binary representation of 3 is <code>&quot;11&quot;</code>.</p>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>1 &lt;= n &lt;= 1000</code></li>
</ul>

<!-- description:end -->

## Solutions

<!-- solution:start -->

### Solution 1

<!-- tabs:start -->

#### Python3

```python
class Solution:
def smallestNumber(self, n: int) -> int:
x = 1
while x - 1 < n:
x <<= 1
return x - 1
```

#### Java

```java
class Solution {
public int smallestNumber(int n) {
int x = 1;
while (x - 1 < n) {
x <<= 1;
}
return x - 1;
}
}
```

#### C++

```cpp
class Solution {
public:
int smallestNumber(int n) {
int x = 1;
while (x - 1 < n) {
x <<= 1;
}
return x - 1;
}
};
```

#### Go

```go
func smallestNumber(n int) int {
x := 1
for x-1 < n {
x <<= 1
}
return x - 1
}
```

#### TypeScript

```ts
function smallestNumber(n: number): number {
let x = 1;
while (x - 1 < n) {
x <<= 1;
}
return x - 1;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Solution {
public:
int smallestNumber(int n) {
int x = 1;
while (x - 1 < n) {
x <<= 1;
}
return x - 1;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
func smallestNumber(n int) int {
x := 1
for x-1 < n {
x <<= 1
}
return x - 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Solution {
public int smallestNumber(int n) {
int x = 1;
while (x - 1 < n) {
x <<= 1;
}
return x - 1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Solution:
def smallestNumber(self, n: int) -> int:
x = 1
while x - 1 < n:
x <<= 1
return x - 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function smallestNumber(n: number): number {
let x = 1;
while (x - 1 < n) {
x <<= 1;
}
return x - 1;
}
Loading
Loading