Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ tags:
<strong>输入:</strong>intervals = [[1,3],[1,4],[2,5],[3,5]]
<strong>输出:</strong>3
<strong>解释:</strong>nums = [2, 3, 4].
可以证明不存在元素数量为 2 的包含集合。
可以证明不存在元素数量为 2 的包含集合。
</pre>

<p><strong class="example">示例 3:</strong></p>
Expand All @@ -54,7 +54,7 @@ tags:
<strong>输入:</strong>intervals = [[1,2],[2,3],[2,4],[4,5]]
<strong>输出:</strong>5
<strong>解释:</strong>nums = [1, 2, 3, 4, 5].
可以证明不存在元素数量为 4 的包含集合。
可以证明不存在元素数量为 4 的包含集合。
</pre>

<p>&nbsp;</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ tags:
<pre>
<strong>输入:</strong>nums = [5,3,6,1,12], original = 3
<strong>输出:</strong>24
<strong>解释:</strong>
<strong>解释:</strong>
- 3 能在 nums 中找到。3 * 2 = 6 。
- 6 能在 nums 中找到。6 * 2 = 12 。
- 12 能在 nums 中找到。12 * 2 = 24 。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ tags:
<pre>
<strong>Input:</strong> nums = [5,3,6,1,12], original = 3
<strong>Output:</strong> 24
<strong>Explanation:</strong>
<strong>Explanation:</strong>
- 3 is found in nums. 3 is multiplied by 2 to obtain 6.
- 6 is found in nums. 6 is multiplied by 2 to obtain 12.
- 12 is found in nums. 12 is multiplied by 2 to obtain 24.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 简单
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3745.Maximize%20Expression%20of%20Three%20Elements/README.md
rating: 1218
source: 第 476 场周赛 Q1
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Easy
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3745.Maximize%20Expression%20of%20Three%20Elements/README_EN.md
rating: 1218
source: Weekly Contest 476 Q1
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3746.Minimum%20String%20Length%20After%20Balanced%20Removals/README.md
rating: 1326
source: 第 476 场周赛 Q2
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3746.Minimum%20String%20Length%20After%20Balanced%20Removals/README_EN.md
rating: 1326
source: Weekly Contest 476 Q2
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3747.Count%20Distinct%20Integers%20After%20Removing%20Zeros/README.md
rating: 1848
source: 第 476 场周赛 Q3
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3747.Count%20Distinct%20Integers%20After%20Removing%20Zeros/README_EN.md
rating: 1848
source: Weekly Contest 476 Q3
---

<!-- problem:start -->
Expand Down
2 changes: 2 additions & 0 deletions solution/3700-3799/3748.Count Stable Subarrays/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: 困难
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3748.Count%20Stable%20Subarrays/README.md
rating: 2209
source: 第 476 场周赛 Q4
---

<!-- problem:start -->
Expand Down
2 changes: 2 additions & 0 deletions solution/3700-3799/3748.Count Stable Subarrays/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
comments: true
difficulty: Hard
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3748.Count%20Stable%20Subarrays/README_EN.md
rating: 2209
source: Weekly Contest 476 Q4
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
---
comments: true
difficulty: 简单
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3750.Minimum%20Number%20of%20Flips%20to%20Reverse%20Binary%20String/README.md
---

<!-- problem:start -->

# [3750. 最少反转次数得到翻转二进制字符串](https://leetcode.cn/problems/minimum-number-of-flips-to-reverse-binary-string)

[English Version](/solution/3700-3799/3750.Minimum%20Number%20of%20Flips%20to%20Reverse%20Binary%20String/README_EN.md)

## 题目描述

<!-- description:start -->

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

<p>令 <code>s</code> 为 <code>n</code> 的 <strong>二进制表示</strong>(不含前导零)。</p>

<p>二进制字符串 <code>s</code> 的 <strong>反转</strong> 是指将 <code>s</code> 中的字符按相反顺序排列得到的字符串。</p>

<p>你可以翻转 <code>s</code> 中的任意一位(将 <code>0 → 1</code> 或 <code>1 → 0</code>)。每次翻转 <strong>仅</strong> 影响一位。</p>

<p>请返回使 <code>s</code> 等于其原始形式的反转所需的 <strong>最少</strong> 翻转次数。</p>

<p>&nbsp;</p>

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

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

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

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

<p>7 的二进制表示为 <code>"111"</code>。其反转也是 <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">4</span></p>

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

<p>10 的二进制表示为 <code>"1010"</code>。其反转为 <code>"0101"</code>。必须翻转所有四位才能使它们相等。因此,所需的最少翻转次数为 4。</p>
</div>

<p>&nbsp;</p>

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

<ul>
<li><code>1 &lt;= n &lt;= 10<sup>9</sup></code></li>
</ul>

<!-- description:end -->

## 解法

<!-- solution:start -->

### 方法一:模拟

我们首先将整数 $n$ 转换成二进制字符串 $s$,然后我们使用双指针分别从字符串的两端向中间遍历,统计对应位置字符不同的个数 $cnt$。由于每次翻转只能影响一位,因此总的翻转次数为 $cnt \times 2$。

时间复杂度 $O(\log n)$,空间复杂度 $O(\log n)$,其中 $n$ 是输入的整数。

<!-- tabs:start -->

#### Python3

```python
class Solution:
def minimumFlips(self, n: int) -> int:
s = bin(n)[2:]
m = len(s)
return sum(s[i] != s[m - i - 1] for i in range(m // 2)) * 2
```

#### Java

```java
class Solution {
public int minimumFlips(int n) {
String s = Integer.toBinaryString(n);
int m = s.length();
int cnt = 0;
for (int i = 0; i < m / 2; i++) {
if (s.charAt(i) != s.charAt(m - i - 1)) {
cnt++;
}
}
return cnt * 2;
}
}
```

#### C++

```cpp
class Solution {
public:
int minimumFlips(int n) {
vector<int> s;
while (n > 0) {
s.push_back(n & 1);
n >>= 1;
}

int m = s.size();
int cnt = 0;
for (int i = 0; i < m / 2; i++) {
if (s[i] != s[m - i - 1]) {
cnt++;
}
}
return cnt * 2;
}
};
```

#### Go

```go
func minimumFlips(n int) int {
s := strconv.FormatInt(int64(n), 2)
m := len(s)
cnt := 0
for i := 0; i < m/2; i++ {
if s[i] != s[m-i-1] {
cnt++
}
}
return cnt * 2
}
```

#### TypeScript

```ts
function minimumFlips(n: number): number {
const s = n.toString(2);
const m = s.length;
let cnt = 0;
for (let i = 0; i < m / 2; i++) {
if (s[i] !== s[m - i - 1]) {
cnt++;
}
}
return cnt * 2;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Loading