Skip to content

Commit 90f466e

Browse files
authored
feat: add new lc problem (#4522)
1 parent ac084bb commit 90f466e

File tree

4 files changed

+220
-0
lines changed

4 files changed

+220
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
---
2+
comments: true
3+
difficulty: 中等
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3595.Once%20Twice/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3595. 一次或两次 🔒](https://leetcode.cn/problems/once-twice)
10+
11+
[English Version](/solution/3500-3599/3595.Once%20Twice/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p>给定一个整数数组&nbsp;<code>nums</code>。在这个数组中:</p>
18+
19+
<ul>
20+
<li>
21+
<p>有一个元素出现了 <strong>恰好 1&nbsp;</strong><strong>次</strong>。</p>
22+
</li>
23+
<li>
24+
<p>有一个元素出现了 <strong>恰好 2&nbsp;</strong><strong>次</strong>。</p>
25+
</li>
26+
<li>
27+
<p>其它所有元素都出现了 <strong>恰好 3 次</strong>。</p>
28+
</li>
29+
</ul>
30+
31+
<p>返回一个长度为 2 的整数数组,其中第一个元素是只出现 <strong>1 次</strong>&nbsp;的那个元素,第二个元素是只出现 <strong>2 次</strong>&nbsp;的那个元素。</p>
32+
33+
<p>你的解决方案必须在 <strong>O(n) 时间</strong>&nbsp;与 <strong>O(1)</strong>&nbsp;空间中运行。</p>
34+
35+
<p>&nbsp;</p>
36+
37+
<p><strong class="example">示例 1:</strong></p>
38+
39+
<div class="example-block">
40+
<p><strong>输入:</strong><span class="example-io">nums = [2,2,3,2,5,5,5,7,7]</span></p>
41+
42+
<p><span class="example-io"><b>输出:</b>[3,7]</span></p>
43+
44+
<p><strong>解释:</strong></p>
45+
46+
<p>元素 3&nbsp;出现了 <strong>1 次</strong>,元素 7&nbsp;出现了 <strong>2 次</strong>。其余所有元素都出现了 <strong>3 次</strong>。</p>
47+
</div>
48+
49+
<p><strong class="example">示例 2:</strong></p>
50+
51+
<div class="example-block">
52+
<p><span class="example-io"><b>输入:</b>nums = [4,4,6,4,9,9,9,6,8]</span></p>
53+
54+
<p><span class="example-io"><b>输出:</b>[8,6]</span></p>
55+
56+
<p><strong>解释:</strong></p>
57+
58+
<p>元素 8 出现了 <strong>1 次</strong>,元素 6 出现了 <strong>2 次</strong>。其余所有元素都出现了 <strong>3 次</strong>。</p>
59+
</div>
60+
61+
<p>&nbsp;</p>
62+
63+
<p><strong>提示:</strong></p>
64+
65+
<ul>
66+
<li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
67+
<li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li>
68+
<li><code>nums.length</code>&nbsp;是 3 的倍数。</li>
69+
<li>恰好有一个元素出现 1 次,一个元素出现 2 次,其余所有元素都出现了 3 次。</li>
70+
</ul>
71+
72+
<!-- description:end -->
73+
74+
## 解法
75+
76+
<!-- solution:start -->
77+
78+
### 方法一
79+
80+
<!-- tabs:start -->
81+
82+
#### Python3
83+
84+
```python
85+
86+
```
87+
88+
#### Java
89+
90+
```java
91+
92+
```
93+
94+
#### C++
95+
96+
```cpp
97+
98+
```
99+
100+
#### Go
101+
102+
```go
103+
104+
```
105+
106+
<!-- tabs:end -->
107+
108+
<!-- solution:end -->
109+
110+
<!-- problem:end -->
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
---
2+
comments: true
3+
difficulty: Medium
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3595.Once%20Twice/README_EN.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3595. Once Twice 🔒](https://leetcode.com/problems/once-twice)
10+
11+
[中文文档](/solution/3500-3599/3595.Once%20Twice/README.md)
12+
13+
## Description
14+
15+
<!-- description:start -->
16+
17+
<p>You are given an integer array <code>nums</code>. In this array:</p>
18+
19+
<ul>
20+
<li>
21+
<p>Exactly one element appears <strong>once</strong>.</p>
22+
</li>
23+
<li>
24+
<p>Exactly one element appears <strong>twice</strong>.</p>
25+
</li>
26+
<li>
27+
<p>All other elements appear <strong>exactly three times</strong>.</p>
28+
</li>
29+
</ul>
30+
31+
<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>
32+
33+
<p>Your solution must run in <strong>O(n)</strong> time and <strong>O(1)</strong> space.</p>
34+
35+
<p>&nbsp;</p>
36+
<p><strong class="example">Example 1:</strong></p>
37+
38+
<div class="example-block">
39+
<p><strong>Input:</strong> <span class="example-io">nums = [2,2,3,2,5,5,5,7,7]</span></p>
40+
41+
<p><strong>Output:</strong> <span class="example-io">[3,7]</span></p>
42+
43+
<p><strong>Explanation:</strong></p>
44+
45+
<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>
46+
</div>
47+
48+
<p><strong class="example">Example 2:</strong></p>
49+
50+
<div class="example-block">
51+
<p><strong>Input:</strong> <span class="example-io">nums = [4,4,6,4,9,9,9,6,8]</span></p>
52+
53+
<p><strong>Output:</strong> <span class="example-io">[8,6]</span></p>
54+
55+
<p><strong>Explanation:</strong></p>
56+
57+
<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>
58+
</div>
59+
60+
<p>&nbsp;</p>
61+
<p><strong>Constraints:</strong></p>
62+
63+
<ul>
64+
<li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
65+
<li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li>
66+
<li><code>nums.length</code> is a multiple of 3.</li>
67+
<li>Exactly one element appears once, one element appears twice, and all other elements appear three times.</li>
68+
</ul>
69+
70+
<!-- description:end -->
71+
72+
## Solutions
73+
74+
<!-- solution:start -->
75+
76+
### Solution 1
77+
78+
<!-- tabs:start -->
79+
80+
#### Python3
81+
82+
```python
83+
84+
```
85+
86+
#### Java
87+
88+
```java
89+
90+
```
91+
92+
#### C++
93+
94+
```cpp
95+
96+
```
97+
98+
#### Go
99+
100+
```go
101+
102+
```
103+
104+
<!-- tabs:end -->
105+
106+
<!-- solution:end -->
107+
108+
<!-- problem:end -->

solution/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3605,6 +3605,7 @@
36053605
| 3592 | [硬币面值还原](/solution/3500-3599/3592.Inverse%20Coin%20Change/README.md) | | 中等 | 第 455 场周赛 |
36063606
| 3593 | [使叶子路径成本相等的最小增量](/solution/3500-3599/3593.Minimum%20Increments%20to%20Equalize%20Leaf%20Paths/README.md) | | 中等 | 第 455 场周赛 |
36073607
| 3594 | [所有人渡河所需的最短时间](/solution/3500-3599/3594.Minimum%20Time%20to%20Transport%20All%20Individuals/README.md) | | 困难 | 第 455 场周赛 |
3608+
| 3595 | [一次或两次](/solution/3500-3599/3595.Once%20Twice/README.md) | | 中等 | 🔒 |
36083609

36093610
## 版权
36103611

solution/README_EN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3603,6 +3603,7 @@ Press <kbd>Control</kbd> + <kbd>F</kbd>(or <kbd>Command</kbd> + <kbd>F</kbd> on
36033603
| 3592 | [Inverse Coin Change](/solution/3500-3599/3592.Inverse%20Coin%20Change/README_EN.md) | | Medium | Weekly Contest 455 |
36043604
| 3593 | [Minimum Increments to Equalize Leaf Paths](/solution/3500-3599/3593.Minimum%20Increments%20to%20Equalize%20Leaf%20Paths/README_EN.md) | | Medium | Weekly Contest 455 |
36053605
| 3594 | [Minimum Time to Transport All Individuals](/solution/3500-3599/3594.Minimum%20Time%20to%20Transport%20All%20Individuals/README_EN.md) | | Hard | Weekly Contest 455 |
3606+
| 3595 | [Once Twice](/solution/3500-3599/3595.Once%20Twice/README_EN.md) | | Medium | 🔒 |
36063607

36073608
## Copyright
36083609

0 commit comments

Comments
 (0)