feat: add new lc problem (#4522)

This commit is contained in:
Libin YANG 2025-06-25 07:19:36 +08:00 committed by GitHub
parent ac084bb7a9
commit 90f466ef1e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 220 additions and 0 deletions

View File

@ -0,0 +1,110 @@
---
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3595.Once%20Twice/README.md
---
<!-- problem:start -->
# [3595. 一次或两次 🔒](https://leetcode.cn/problems/once-twice)
[English Version](/solution/3500-3599/3595.Once%20Twice/README_EN.md)
## 题目描述
<!-- description:start -->
<p>给定一个整数数组&nbsp;<code>nums</code>。在这个数组中:</p>
<ul>
<li>
<p>有一个元素出现了 <strong>恰好 1&nbsp;</strong><strong></strong></p>
</li>
<li>
<p>有一个元素出现了 <strong>恰好 2&nbsp;</strong><strong></strong></p>
</li>
<li>
<p>其它所有元素都出现了 <strong>恰好 3 次</strong></p>
</li>
</ul>
<p>返回一个长度为 2 的整数数组,其中第一个元素是只出现 <strong>1 次</strong>&nbsp;的那个元素,第二个元素是只出现 <strong>2 次</strong>&nbsp;的那个元素。</p>
<p>你的解决方案必须在 <strong>O(n) 时间</strong>&nbsp;<strong>O(1)</strong>&nbsp;空间中运行。</p>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<div class="example-block">
<p><strong>输入:</strong><span class="example-io">nums = [2,2,3,2,5,5,5,7,7]</span></p>
<p><span class="example-io"><b>输出:</b>[3,7]</span></p>
<p><strong>解释:</strong></p>
<p>元素 3&nbsp;出现了 <strong>1 次</strong>,元素 7&nbsp;出现了 <strong>2 次</strong>。其余所有元素都出现了 <strong>3 次</strong></p>
</div>
<p><strong class="example">示例 2</strong></p>
<div class="example-block">
<p><span class="example-io"><b>输入:</b>nums = [4,4,6,4,9,9,9,6,8]</span></p>
<p><span class="example-io"><b>输出:</b>[8,6]</span></p>
<p><strong>解释:</strong></p>
<p>元素 8 出现了 <strong>1 次</strong>,元素 6 出现了 <strong>2 次</strong>。其余所有元素都出现了 <strong>3 次</strong></p>
</div>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li>
<li><code>nums.length</code>&nbsp;是 3 的倍数。</li>
<li>恰好有一个元素出现 1 次,一个元素出现 2 次,其余所有元素都出现了 3 次。</li>
</ul>
<!-- description:end -->
## 解法
<!-- solution:start -->
### 方法一
<!-- tabs:start -->
#### Python3
```python
```
#### Java
```java
```
#### C++
```cpp
```
#### Go
```go
```
<!-- tabs:end -->
<!-- solution:end -->
<!-- problem:end -->

View File

@ -0,0 +1,108 @@
---
comments: true
difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3595.Once%20Twice/README_EN.md
---
<!-- problem:start -->
# [3595. Once Twice 🔒](https://leetcode.com/problems/once-twice)
[中文文档](/solution/3500-3599/3595.Once%20Twice/README.md)
## Description
<!-- description:start -->
<p>You are given an integer array <code>nums</code>. In this array:</p>
<ul>
<li>
<p>Exactly one element appears <strong>once</strong>.</p>
</li>
<li>
<p>Exactly one element appears <strong>twice</strong>.</p>
</li>
<li>
<p>All other elements appear <strong>exactly three times</strong>.</p>
</li>
</ul>
<p>Return an integer array of length 2, where the first element is the one that appears <strong>once</strong>, and the second is the one that appears <strong>twice</strong>.</p>
<p>Your solution must run in <strong>O(n)</strong> time and <strong>O(1)</strong> space.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,2,3,2,5,5,5,7,7]</span></p>
<p><strong>Output:</strong> <span class="example-io">[3,7]</span></p>
<p><strong>Explanation:</strong></p>
<p>The element 3 appears <b>once</b>, and the element 7 appears <b>twice</b>. The remaining elements each appear <b>three times</b>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [4,4,6,4,9,9,9,6,8]</span></p>
<p><strong>Output:</strong> <span class="example-io">[8,6]</span></p>
<p><strong>Explanation:</strong></p>
<p>The element 8 appears <b>once</b>, and the element 6 appears <b>twice</b>. The remaining elements each appear <b>three times</b>.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li>
<li><code>nums.length</code> is a multiple of 3.</li>
<li>Exactly one element appears once, one element appears twice, and all other elements appear three times.</li>
</ul>
<!-- description:end -->
## Solutions
<!-- solution:start -->
### Solution 1
<!-- tabs:start -->
#### Python3
```python
```
#### Java
```java
```
#### C++
```cpp
```
#### Go
```go
```
<!-- tabs:end -->
<!-- solution:end -->
<!-- problem:end -->

View File

@ -3605,6 +3605,7 @@
| 3592 | [硬币面值还原](/solution/3500-3599/3592.Inverse%20Coin%20Change/README.md) | | 中等 | 第 455 场周赛 |
| 3593 | [使叶子路径成本相等的最小增量](/solution/3500-3599/3593.Minimum%20Increments%20to%20Equalize%20Leaf%20Paths/README.md) | | 中等 | 第 455 场周赛 |
| 3594 | [所有人渡河所需的最短时间](/solution/3500-3599/3594.Minimum%20Time%20to%20Transport%20All%20Individuals/README.md) | | 困难 | 第 455 场周赛 |
| 3595 | [一次或两次](/solution/3500-3599/3595.Once%20Twice/README.md) | | 中等 | 🔒 |
## 版权

View File

@ -3603,6 +3603,7 @@ Press <kbd>Control</kbd> + <kbd>F</kbd>(or <kbd>Command</kbd> + <kbd>F</kbd> on
| 3592 | [Inverse Coin Change](/solution/3500-3599/3592.Inverse%20Coin%20Change/README_EN.md) | | Medium | Weekly Contest 455 |
| 3593 | [Minimum Increments to Equalize Leaf Paths](/solution/3500-3599/3593.Minimum%20Increments%20to%20Equalize%20Leaf%20Paths/README_EN.md) | | Medium | Weekly Contest 455 |
| 3594 | [Minimum Time to Transport All Individuals](/solution/3500-3599/3594.Minimum%20Time%20to%20Transport%20All%20Individuals/README_EN.md) | | Hard | Weekly Contest 455 |
| 3595 | [Once Twice](/solution/3500-3599/3595.Once%20Twice/README_EN.md) | | Medium | 🔒 |
## Copyright