File tree 6 files changed +149
-42
lines changed
solution/0900-0999/0961.N-Repeated Element in Size 2N Array
6 files changed +149
-42
lines changed Original file line number Diff line number Diff line change 51
51
52
52
<!-- 这里可写通用的实现逻辑 -->
53
53
54
+ 长度为 ` 2N ` ,共 ` N+1 ` 个不同元素,其中一个元素出现 ` N ` 次,说明其它元素各不相同。
55
+
56
+ 遍历数组,只要出现重复元素,它就是我们要找的重复 ` N ` 次的元素。
57
+
54
58
<!-- tabs:start -->
55
59
56
60
### ** Python3**
57
61
58
62
<!-- 这里可写当前语言的特殊实现逻辑 -->
59
63
60
64
``` python
61
-
65
+ class Solution :
66
+ def repeatedNTimes (self , nums : List[int ]) -> int :
67
+ s = set ()
68
+ for num in nums:
69
+ if num in s:
70
+ return num
71
+ s.add(num)
62
72
```
63
73
64
74
### ** Java**
65
75
66
76
<!-- 这里可写当前语言的特殊实现逻辑 -->
67
77
68
78
``` java
79
+ class Solution {
80
+ public int repeatedNTimes (int [] nums ) {
81
+ Set<Integer > s = new HashSet<> ();
82
+ for (int num : nums) {
83
+ if (s. contains(num)) {
84
+ return num;
85
+ }
86
+ s. add(num);
87
+ }
88
+ return - 1 ;
89
+ }
90
+ }
91
+ ```
92
+
93
+ ### ** C++**
94
+
95
+ ``` cpp
96
+ class Solution {
97
+ public:
98
+ int repeatedNTimes(vector<int >& nums) {
99
+ unordered_set<int > s;
100
+ for (auto &num : nums) {
101
+ if (s.find(num) != s.end()) {
102
+ return num;
103
+ }
104
+ s.insert(num);
105
+ }
106
+ return -1;
107
+ }
108
+ };
109
+ ```
69
110
111
+ ### **JavaScript**
112
+
113
+ ```js
114
+ /**
115
+ * @param {number[]} nums
116
+ * @return {number}
117
+ */
118
+ var repeatedNTimes = function(nums) {
119
+ const s = new Set();
120
+ for (const num of nums) {
121
+ if (s.has(num)) {
122
+ return num;
123
+ }
124
+ s.add(num);
125
+ }
126
+ return -1;
127
+ };
70
128
```
71
129
72
130
### ** ...**
Original file line number Diff line number Diff line change 58
58
### ** Python3**
59
59
60
60
``` python
61
-
61
+ class Solution :
62
+ def repeatedNTimes (self , nums : List[int ]) -> int :
63
+ s = set ()
64
+ for num in nums:
65
+ if num in s:
66
+ return num
67
+ s.add(num)
62
68
```
63
69
64
70
### ** Java**
65
71
66
72
``` java
73
+ class Solution {
74
+ public int repeatedNTimes (int [] nums ) {
75
+ Set<Integer > s = new HashSet<> ();
76
+ for (int num : nums) {
77
+ if (s. contains(num)) {
78
+ return num;
79
+ }
80
+ s. add(num);
81
+ }
82
+ return - 1 ;
83
+ }
84
+ }
85
+ ```
86
+
87
+ ### ** C++**
88
+
89
+ ``` cpp
90
+ class Solution {
91
+ public:
92
+ int repeatedNTimes(vector<int >& nums) {
93
+ unordered_set<int > s;
94
+ for (auto &num : nums) {
95
+ if (s.find(num) != s.end()) {
96
+ return num;
97
+ }
98
+ s.insert(num);
99
+ }
100
+ return -1;
101
+ }
102
+ };
103
+ ```
67
104
105
+ ### **JavaScript**
106
+
107
+ ```js
108
+ /**
109
+ * @param {number[]} nums
110
+ * @return {number}
111
+ */
112
+ var repeatedNTimes = function(nums) {
113
+ const s = new Set();
114
+ for (const num of nums) {
115
+ if (s.has(num)) {
116
+ return num;
117
+ }
118
+ s.add(num);
119
+ }
120
+ return -1;
121
+ };
68
122
```
69
123
70
124
### ** ...**
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public:
3
- int repeatedNTimes (vector<int >& A) {
4
- unordered_set<int > us (A[0 ]) ;
5
- for (int i = (A.size () >> 1 ) - 1 ; i < A.size (); ++i)
6
- if (us.find (A[i]) != us.end ())
7
- return A[i] ;
8
- else
9
- us.insert (A[i]) ;
10
- return A[0 ] ;
3
+ int repeatedNTimes (vector<int >& nums) {
4
+ unordered_set<int > s;
5
+ for (auto &num : nums) {
6
+ if (s.find (num) != s.end ()) {
7
+ return num;
8
+ }
9
+ s.insert (num);
10
+ }
11
+ return -1 ;
11
12
}
12
13
};
Original file line number Diff line number Diff line change 1
1
class Solution {
2
- public int repeatedNTimes (int [] A ) {
3
- Set <Integer > set = new HashSet <>();
4
- for (int e : A ) {
5
- if (set .contains (e )) {
6
- return e ;
2
+ public int repeatedNTimes (int [] nums ) {
3
+ Set <Integer > s = new HashSet <>();
4
+ for (int num : nums ) {
5
+ if (s .contains (num )) {
6
+ return num ;
7
7
}
8
- set .add (e );
8
+ s .add (num );
9
9
}
10
- return 0 ;
10
+ return - 1 ;
11
11
}
12
12
}
Original file line number Diff line number Diff line change 1
- const repeatedNTimes = function ( A ) {
2
- let ss = new Set ( ) ;
3
- for ( let i = 0 ; i < A . length ; i ++ ) {
4
- if ( ss . has ( A [ i ] ) ) {
5
- return A [ i ] ;
6
- }
7
- ss . add ( A [ i ] ) ;
1
+ /**
2
+ * @param {number[] } nums
3
+ * @return {number }
4
+ */
5
+ var repeatedNTimes = function ( nums ) {
6
+ const s = new Set ( ) ;
7
+ for ( const num of nums ) {
8
+ if ( s . has ( num ) ) {
9
+ return num ;
10
+ }
11
+ s . add ( num ) ;
8
12
}
9
- return null ;
10
- } ;
13
+ return - 1 ;
14
+ } ;
Original file line number Diff line number Diff line change 1
1
class Solution :
2
- def repeatedNTimes (self , A ):
3
- """
4
- :type A: List[int]
5
- :rtype: int
6
- """
7
- if A [0 ] == A [1 ] or A [0 ] == A [2 ] or A [0 ] == A [3 ]:
8
- return A [0 ]
9
- elif A [1 ] == A [2 ] or A [1 ] == A [3 ]:
10
- return A [1 ]
11
- elif A [2 ] == A [3 ]:
12
- return A [2 ]
13
- i = 4
14
- while i < len (A ):
15
- if A [i ] == A [i + 1 ]:
16
- return A [i ]
17
- i += 2
2
+ def repeatedNTimes (self , nums : List [int ]) -> int :
3
+ s = set ()
4
+ for num in nums :
5
+ if num in s :
6
+ return num
7
+ s .add (num )
You can’t perform that action at this time.
0 commit comments