From 8339256dcf11a7d27813040a59741838a942bf8c Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 5 Dec 2025 20:33:12 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.0961 --- .../README.md | 109 ++++++++++++++++- .../README_EN.md | 111 +++++++++++++++++- .../Solution2.cpp | 11 ++ .../Solution2.go | 8 ++ .../Solution2.java | 10 ++ .../Solution2.js | 12 ++ .../Solution2.py | 6 + .../Solution2.ts | 8 ++ 8 files changed, 271 insertions(+), 4 deletions(-) create mode 100644 solution/0900-0999/0961.N-Repeated Element in Size 2N Array/Solution2.cpp create mode 100644 solution/0900-0999/0961.N-Repeated Element in Size 2N Array/Solution2.go create mode 100644 solution/0900-0999/0961.N-Repeated Element in Size 2N Array/Solution2.java create mode 100644 solution/0900-0999/0961.N-Repeated Element in Size 2N Array/Solution2.js create mode 100644 solution/0900-0999/0961.N-Repeated Element in Size 2N Array/Solution2.py create mode 100644 solution/0900-0999/0961.N-Repeated Element in Size 2N Array/Solution2.ts diff --git a/solution/0900-0999/0961.N-Repeated Element in Size 2N Array/README.md b/solution/0900-0999/0961.N-Repeated Element in Size 2N Array/README.md index 278e9c78061cf..dffd125708916 100644 --- a/solution/0900-0999/0961.N-Repeated Element in Size 2N Array/README.md +++ b/solution/0900-0999/0961.N-Repeated Element in Size 2N Array/README.md @@ -73,11 +73,11 @@ tags: ### 方法一:哈希表 -由于数组 $nums$ 一共有 $2n$ 个元素,其中有 $n + 1$ 个不同的元素,且有一个元素重复了 $n$ 次,说明数组中的其余 $n$ 个元素都是不同的。 +由于数组 $\textit{nums}$ 一共有 $2n$ 个元素,其中有 $n + 1$ 个不同的元素,且有一个元素重复了 $n$ 次,说明数组中的其余 $n$ 个元素都是不同的。 -因此,我们只需要遍历数组 $nums$,用哈希表 $s$ 记录遍历过的元素。当遍历到某个元素 $x$ 时,如果 $x$ 在哈希表 $s$ 中已经存在,说明 $x$ 是重复的元素,直接返回 $x$ 即可。 +因此,我们只需要遍历数组 $\textit{nums}$,用哈希表 $s$ 记录遍历过的元素。当遍历到某个元素 $x$ 时,如果 $x$ 在哈希表 $s$ 中已经存在,说明 $x$ 是重复的元素,直接返回 $x$ 即可。 -时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $nums$ 的长度。 +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{nums}$ 的长度。 @@ -175,4 +175,107 @@ var repeatedNTimes = function (nums) { + + +### 方法二:数学 + +根据题目描述,数组 $\textit{nums}$ 中有一半的元素都是相同的,如果我们将数组视为环形排列,那么两个相同元素之间的最多间隔 $1$ 个其他元素。 + +因此,我们从下标 $2$ 开始遍历数组 $\textit{nums}$,对于每个下标 $i$,我们比较 $\textit{nums}[i]$ 与 $\textit{nums}[i - 1]$ 和 $\textit{nums}[i - 2]$ 的值,如果相等则返回该值。 + +如果没有在上述过程中找到重复的元素,那么重复的元素一定是 $\textit{nums}[0]$,我们直接返回 $\textit{nums}[0]$ 即可。 + +时间复杂度 $O(n)$,其中 $n$ 为数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。 + + + +#### Python3 + +```python +class Solution: + def repeatedNTimes(self, nums: List[int]) -> int: + for i in range(2, len(nums)): + if nums[i] == nums[i - 1] or nums[i] == nums[i - 2]: + return nums[i] + return nums[0] +``` + +#### Java + +```java +class Solution { + public int repeatedNTimes(int[] nums) { + for (int i = 2; i < nums.length; ++i) { + if (nums[i] == nums[i - 1] || nums[i] == nums[i - 2]) { + return nums[i]; + } + } + return nums[0]; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + int repeatedNTimes(vector& nums) { + for (int i = 2; i < nums.size(); ++i) { + if (nums[i] == nums[i - 1] || nums[i] == nums[i - 2]) { + return nums[i]; + } + } + return nums[0]; + } +}; +``` + +#### Go + +```go +func repeatedNTimes(nums []int) int { + for i := 2; i < len(nums); i++ { + if nums[i] == nums[i-1] || nums[i] == nums[i-2] { + return nums[i] + } + } + return nums[0] +} +``` + +#### TypeScript + +```ts +function repeatedNTimes(nums: number[]): number { + for (let i = 2; i < nums.length; ++i) { + if (nums[i] === nums[i - 1] || nums[i] === nums[i - 2]) { + return nums[i]; + } + } + return nums[0]; +} +``` + +#### JavaScript + +```js +/** + * @param {number[]} nums + * @return {number} + */ +var repeatedNTimes = function (nums) { + for (let i = 2; i < nums.length; ++i) { + if (nums[i] === nums[i - 1] || nums[i] === nums[i - 2]) { + return nums[i]; + } + } + return nums[0]; +}; +``` + + + + + diff --git a/solution/0900-0999/0961.N-Repeated Element in Size 2N Array/README_EN.md b/solution/0900-0999/0961.N-Repeated Element in Size 2N Array/README_EN.md index bd29e0e2cd0be..6de562478c81d 100644 --- a/solution/0900-0999/0961.N-Repeated Element in Size 2N Array/README_EN.md +++ b/solution/0900-0999/0961.N-Repeated Element in Size 2N Array/README_EN.md @@ -54,7 +54,13 @@ tags: -### Solution 1 +### Solution 1: Hash Table + +Since the array $\textit{nums}$ has a total of $2n$ elements, with $n + 1$ distinct elements, and one element repeated $n$ times, this means the remaining $n$ elements in the array are all distinct. + +Therefore, we only need to iterate through the array $\textit{nums}$ and use a hash table $s$ to record the elements we've encountered. When we encounter an element $x$, if $x$ already exists in the hash table $s$, it means $x$ is the repeated element, and we can directly return $x$. + +The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the array $\textit{nums}$. @@ -152,4 +158,107 @@ var repeatedNTimes = function (nums) { + + +### Solution 2: Mathematics + +According to the problem description, half of the elements in the array $\textit{nums}$ are the same. If we view the array as a circular arrangement, then there is at most $1$ other element between two identical elements. + +Therefore, we iterate through the array $\textit{nums}$ starting from index $2$. For each index $i$, we compare $\textit{nums}[i]$ with $\textit{nums}[i - 1]$ and $\textit{nums}[i - 2]$. If they are equal, we return that value. + +If we don't find the repeated element in the above process, then the repeated element must be $\textit{nums}[0]$, and we can directly return $\textit{nums}[0]$. + +The time complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. The space complexity is $O(1)$. + + + +#### Python3 + +```python +class Solution: + def repeatedNTimes(self, nums: List[int]) -> int: + for i in range(2, len(nums)): + if nums[i] == nums[i - 1] or nums[i] == nums[i - 2]: + return nums[i] + return nums[0] +``` + +#### Java + +```java +class Solution { + public int repeatedNTimes(int[] nums) { + for (int i = 2; i < nums.length; ++i) { + if (nums[i] == nums[i - 1] || nums[i] == nums[i - 2]) { + return nums[i]; + } + } + return nums[0]; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + int repeatedNTimes(vector& nums) { + for (int i = 2; i < nums.size(); ++i) { + if (nums[i] == nums[i - 1] || nums[i] == nums[i - 2]) { + return nums[i]; + } + } + return nums[0]; + } +}; +``` + +#### Go + +```go +func repeatedNTimes(nums []int) int { + for i := 2; i < len(nums); i++ { + if nums[i] == nums[i-1] || nums[i] == nums[i-2] { + return nums[i] + } + } + return nums[0] +} +``` + +#### TypeScript + +```ts +function repeatedNTimes(nums: number[]): number { + for (let i = 2; i < nums.length; ++i) { + if (nums[i] === nums[i - 1] || nums[i] === nums[i - 2]) { + return nums[i]; + } + } + return nums[0]; +} +``` + +#### JavaScript + +```js +/** + * @param {number[]} nums + * @return {number} + */ +var repeatedNTimes = function (nums) { + for (let i = 2; i < nums.length; ++i) { + if (nums[i] === nums[i - 1] || nums[i] === nums[i - 2]) { + return nums[i]; + } + } + return nums[0]; +}; +``` + + + + + diff --git a/solution/0900-0999/0961.N-Repeated Element in Size 2N Array/Solution2.cpp b/solution/0900-0999/0961.N-Repeated Element in Size 2N Array/Solution2.cpp new file mode 100644 index 0000000000000..647a958e0f608 --- /dev/null +++ b/solution/0900-0999/0961.N-Repeated Element in Size 2N Array/Solution2.cpp @@ -0,0 +1,11 @@ +class Solution { +public: + int repeatedNTimes(vector& nums) { + for (int i = 2; i < nums.size(); ++i) { + if (nums[i] == nums[i - 1] || nums[i] == nums[i - 2]) { + return nums[i]; + } + } + return nums[0]; + } +}; diff --git a/solution/0900-0999/0961.N-Repeated Element in Size 2N Array/Solution2.go b/solution/0900-0999/0961.N-Repeated Element in Size 2N Array/Solution2.go new file mode 100644 index 0000000000000..bebd14bdd5ff9 --- /dev/null +++ b/solution/0900-0999/0961.N-Repeated Element in Size 2N Array/Solution2.go @@ -0,0 +1,8 @@ +func repeatedNTimes(nums []int) int { + for i := 2; i < len(nums); i++ { + if nums[i] == nums[i-1] || nums[i] == nums[i-2] { + return nums[i] + } + } + return nums[0] +} diff --git a/solution/0900-0999/0961.N-Repeated Element in Size 2N Array/Solution2.java b/solution/0900-0999/0961.N-Repeated Element in Size 2N Array/Solution2.java new file mode 100644 index 0000000000000..f49edf6e834e0 --- /dev/null +++ b/solution/0900-0999/0961.N-Repeated Element in Size 2N Array/Solution2.java @@ -0,0 +1,10 @@ +class Solution { + public int repeatedNTimes(int[] nums) { + for (int i = 2; i < nums.length; ++i) { + if (nums[i] == nums[i - 1] || nums[i] == nums[i - 2]) { + return nums[i]; + } + } + return nums[0]; + } +} diff --git a/solution/0900-0999/0961.N-Repeated Element in Size 2N Array/Solution2.js b/solution/0900-0999/0961.N-Repeated Element in Size 2N Array/Solution2.js new file mode 100644 index 0000000000000..122f6ff1f8f5f --- /dev/null +++ b/solution/0900-0999/0961.N-Repeated Element in Size 2N Array/Solution2.js @@ -0,0 +1,12 @@ +/** + * @param {number[]} nums + * @return {number} + */ +var repeatedNTimes = function (nums) { + for (let i = 2; i < nums.length; ++i) { + if (nums[i] === nums[i - 1] || nums[i] === nums[i - 2]) { + return nums[i]; + } + } + return nums[0]; +}; diff --git a/solution/0900-0999/0961.N-Repeated Element in Size 2N Array/Solution2.py b/solution/0900-0999/0961.N-Repeated Element in Size 2N Array/Solution2.py new file mode 100644 index 0000000000000..5ffe66ce1c7e4 --- /dev/null +++ b/solution/0900-0999/0961.N-Repeated Element in Size 2N Array/Solution2.py @@ -0,0 +1,6 @@ +class Solution: + def repeatedNTimes(self, nums: List[int]) -> int: + for i in range(2, len(nums)): + if nums[i] == nums[i - 1] or nums[i] == nums[i - 2]: + return nums[i] + return nums[0] diff --git a/solution/0900-0999/0961.N-Repeated Element in Size 2N Array/Solution2.ts b/solution/0900-0999/0961.N-Repeated Element in Size 2N Array/Solution2.ts new file mode 100644 index 0000000000000..f8ee0e27d514a --- /dev/null +++ b/solution/0900-0999/0961.N-Repeated Element in Size 2N Array/Solution2.ts @@ -0,0 +1,8 @@ +function repeatedNTimes(nums: number[]): number { + for (let i = 2; i < nums.length; ++i) { + if (nums[i] === nums[i - 1] || nums[i] === nums[i - 2]) { + return nums[i]; + } + } + return nums[0]; +}