Skip to content

Commit e6807e1

Browse files
committed
✨feat: Add 961
1 parent e064f35 commit e6807e1

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed

Index/哈希表.md

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
| [888. 公平的糖果棒交换](https://leetcode-cn.com/problems/fair-candy-swap/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/fair-candy-swap/solution/gong-shui-san-xie-yi-ti-shuang-jie-po-su-uant/) | 简单 | 🤩🤩 |
4545
| [930. 和相同的二元子数组](https://leetcode-cn.com/problems/binary-subarrays-with-sum/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/binary-subarrays-with-sum/solution/gong-shui-san-xie-yi-ti-shuang-jie-qian-hfoc0/) | 中等 | 🤩🤩🤩 |
4646
| [954. 二倍数对数组](https://leetcode-cn.com/problems/array-of-doubled-pairs/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/array-of-doubled-pairs/solution/by-ac_oier-d1z7/) | 中等 | 🤩🤩🤩 |
47+
| [961. 在长度 2N 的数组中找出重复 N 次的元素](https://leetcode.cn/problems/n-repeated-element-in-size-2n-array/) | [LeetCode 题解链接](https://leetcode.cn/problems/n-repeated-element-in-size-2n-array/solution/by-ac_oier-bslq/) | 简单 | 🤩🤩🤩🤩 |
4748
| [981. 基于时间的键值存储](https://leetcode-cn.com/problems/time-based-key-value-store/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/time-based-key-value-store/solution/gong-shui-san-xie-yi-ti-shuang-jie-ha-xi-h5et/) | 中等 | 🤩🤩🤩🤩 |
4849
| [987. 二叉树的垂序遍历](https://leetcode-cn.com/problems/vertical-order-traversal-of-a-binary-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/vertical-order-traversal-of-a-binary-tree/solution/gong-shui-san-xie-yi-ti-shuang-jie-dfs-h-wfm3/) | 困难 | 🤩🤩🤩 |
4950
| [1001. 网格照明](https://leetcode-cn.com/problems/grid-illumination/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/grid-illumination/solution/gong-shui-san-xie-ha-xi-biao-xian-ying-s-s48d/) | 困难 | 🤩🤩🤩🤩 |

Index/构造.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
| ------------------------------------------------------------ | ------------------------------------------------------------ | ---- | -------- |
33
| [406. 根据身高重建队列](https://leetcode.cn/problems/queue-reconstruction-by-height/) | [LeetCode 题解链接](https://leetcode.cn/problems/queue-reconstruction-by-height/solution/by-ac_oier-fda2/) | 中等 | 🤩🤩🤩🤩 |
44
| [942. 增减字符串匹配](https://leetcode.cn/problems/di-string-match/) | [LeetCode 题解链接](https://leetcode.cn/problems/di-string-match/solution/by-ac_oier-pvjk/) | 简单 | 🤩🤩🤩🤩🤩 |
5+
| [961. 在长度 2N 的数组中找出重复 N 次的元素](https://leetcode.cn/problems/n-repeated-element-in-size-2n-array/) | [LeetCode 题解链接](https://leetcode.cn/problems/n-repeated-element-in-size-2n-array/solution/by-ac_oier-bslq/) | 简单 | 🤩🤩🤩🤩 |
56
| [1719. 重构一棵树的方案数](https://leetcode-cn.com/problems/number-of-ways-to-reconstruct-a-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/number-of-ways-to-reconstruct-a-tree/solution/gong-shui-san-xie-gou-zao-yan-zheng-he-f-q6fc/) | 困难 | 🤩🤩 |
67
| [2028. 找出缺失的观测数据](https://leetcode-cn.com/problems/find-missing-observations/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-missing-observations/solution/by-ac_oier-x22k/) | 中等 | 🤩🤩🤩🤩🤩 |
78

Index/模拟.md

+1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
| [905. 按奇偶排序数组](https://leetcode-cn.com/problems/sort-array-by-parity/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/sort-array-by-parity/solution/by-ac_oier-nuz7/) | 简单 | 🤩🤩🤩 |
107107
| [944. 删列造序](https://leetcode.cn/problems/delete-columns-to-make-sorted/) | [LeetCode 题解链接](https://leetcode.cn/problems/delete-columns-to-make-sorted/solution/by-ac_oier-smoz/) | 简单 | 🤩🤩🤩 |
108108
| [953. 验证外星语词典](https://leetcode.cn/problems/verifying-an-alien-dictionary/) | [LeetCode 题解链接](https://leetcode.cn/problems/verifying-an-alien-dictionary/solution/by-ac_oier-sxf1/) | 简单 | 🤩🤩🤩🤩 |
109+
| [961. 在长度 2N 的数组中找出重复 N 次的元素](https://leetcode.cn/problems/n-repeated-element-in-size-2n-array/) | [LeetCode 题解链接](https://leetcode.cn/problems/n-repeated-element-in-size-2n-array/solution/by-ac_oier-bslq/) | 简单 | 🤩🤩🤩🤩 |
109110
| [997. 找到小镇的法官](https://leetcode-cn.com/problems/find-the-town-judge/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-the-town-judge/solution/gong-shui-san-xie-jian-dan-chu-du-ru-du-5ms57/) | 简单 | 🤩🤩🤩🤩 |
110111
| [1001. 网格照明](https://leetcode-cn.com/problems/grid-illumination/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/grid-illumination/solution/gong-shui-san-xie-ha-xi-biao-xian-ying-s-s48d/) | 困难 | 🤩🤩🤩🤩 |
111112
| [1005. K 次取反后最大化的数组和](https://leetcode-cn.com/problems/maximize-sum-of-array-after-k-negations/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximize-sum-of-array-after-k-negations/solution/gong-shui-san-xie-jian-dan-fen-qing-kuan-6qwu/) | 简单 | 🤩🤩🤩🤩 |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[961. 在长度 2N 的数组中找出重复 N 次的元素](https://leetcode.cn/problems/n-repeated-element-in-size-2n-array/solution/by-ac_oier-bslq/)** ,难度为 **简单**
4+
5+
Tag : 「模拟」、「计数」、「构造」、「哈希表」
6+
7+
8+
9+
给你一个整数数组 `nums` ,该数组具有以下属性:
10+
11+
* $nums.length == 2 \times n$
12+
* `nums` 包含 $n + 1$ 个 不同的 元素
13+
* `nums` 中恰有一个元素重复 $n$ 次
14+
15+
找出并返回重复了 $n$ 次的那个元素。
16+
17+
示例 1:
18+
```
19+
输入:nums = [1,2,3,3]
20+
21+
输出:3
22+
```
23+
示例 2:
24+
```
25+
输入:nums = [2,1,2,5,3,2]
26+
27+
输出:2
28+
```
29+
示例 3:
30+
```
31+
输入:nums = [5,1,5,2,5,3,5,4]
32+
33+
输出:5
34+
```
35+
36+
提示:
37+
* $2 <= n <= 50004
38+
* $nums.length == 2 \times n$
39+
* $0 <= nums[i] <= 10^4$
40+
* `nums` 由 $n + 1$ 个 不同的 元素组成,且其中一个元素恰好重复 $n$ 次
41+
42+
---
43+
44+
### 计数模拟
45+
46+
根据题目给定的三个条件可推断出:数组中仅有一个元素出现多次,其余元素均出现一次。
47+
48+
又利用数据范围为 $10^4$,我们可以使用数组充当哈希表进行计数,当出现一个 $nums[i]$ 重复出现即是答案。
49+
50+
代码:
51+
```Java
52+
class Solution {
53+
int[] cnts = new int[10010];
54+
public int repeatedNTimes(int[] nums) {
55+
for (int x : nums) {
56+
if (++cnts[x] > 1) return x;
57+
}
58+
return -1;
59+
}
60+
}
61+
```
62+
* 时间复杂度:$O(n)$
63+
* 空间复杂度:$O(C)$
64+
65+
---
66+
67+
### 构造共性
68+
69+
假设重复出现的数字是 $x$,数字 $x$ 重复了 $n$ 次,要将这 $n$ 个相同的 $x$ 间隔开,需要 $n - 1$ 个额外数字,而实际上我们除 $x$ 以外还有 $n$ 个额外数字(数字总数为 $n + 1$ 个),因此在我们所能构造出的所有排列方式中,**最多** 使相邻 $x$ 之间间隔了 $2$ 个额外数字。
70+
71+
对于每个 $nums[i]$ 而言,我们检查往前的三个位置(若有),如果有重复相等情况,说明当前的 $nums[i]$ 即是答案。
72+
73+
代码:
74+
```Java
75+
class Solution {
76+
public int repeatedNTimes(int[] nums) {
77+
int n = nums.length;
78+
for (int i = 0; i < n; i++) {
79+
int t = nums[i];
80+
if (i - 1 >= 0 && nums[i - 1] == t) return t;
81+
if (i - 2 >= 0 && nums[i - 2] == t) return t;
82+
if (i - 3 >= 0 && nums[i - 3] == t) return t;
83+
}
84+
return -1;
85+
}
86+
}
87+
```
88+
* 时间复杂度:$O(n)$
89+
* 空间复杂度:$O(1)$
90+
91+
---
92+
93+
### 最后
94+
95+
这是我们「刷穿 LeetCode」系列文章的第 `No.961` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
96+
97+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
98+
99+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
100+
101+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
102+

0 commit comments

Comments
 (0)