File tree 4 files changed +112
-0
lines changed
solution/0200-0299/0260.Single Number III
4 files changed +112
-0
lines changed Original file line number Diff line number Diff line change @@ -238,4 +238,48 @@ public class Solution {
238
238
239
239
<!-- solution: end -->
240
240
241
+ <!-- solution: start -->
242
+
243
+ ### 方法二:哈希表
244
+
245
+ <!-- tabs: start -->
246
+
247
+ #### TypeScript
248
+
249
+ ``` ts
250
+ function singleNumber(nums : number []): number [] {
251
+ const set = new Set <number >();
252
+
253
+ for (const x of nums ) {
254
+ if (set .has (x )) set .delete (x );
255
+ else set .add (x );
256
+ }
257
+
258
+ return [... set ];
259
+ }
260
+ ```
261
+
262
+ #### JavaScript
263
+
264
+ ``` js
265
+ /**
266
+ * @param {number[]} nums
267
+ * @return {number[]}
268
+ */
269
+ function singleNumber (nums ) {
270
+ const set = new Set ();
271
+
272
+ for (const x of nums) {
273
+ if (set .has (x)) set .delete (x);
274
+ else set .add (x);
275
+ }
276
+
277
+ return [... set];
278
+ }
279
+ ```
280
+
281
+ <!-- tabs: end -->
282
+
283
+ <!-- solution: end -->
284
+
241
285
<!-- problem: end -->
Original file line number Diff line number Diff line change @@ -236,4 +236,48 @@ public class Solution {
236
236
237
237
<!-- solution: end -->
238
238
239
+ <!-- solution: start -->
240
+
241
+ ### Solution 2: Hash Table
242
+
243
+ <!-- tabs: start -->
244
+
245
+ #### TypeScript
246
+
247
+ ``` ts
248
+ function singleNumber(nums : number []): number [] {
249
+ const set = new Set <number >();
250
+
251
+ for (const x of nums ) {
252
+ if (set .has (x )) set .delete (x );
253
+ else set .add (x );
254
+ }
255
+
256
+ return [... set ];
257
+ }
258
+ ```
259
+
260
+ #### JavaScript
261
+
262
+ ``` js
263
+ /**
264
+ * @param {number[]} nums
265
+ * @return {number[]}
266
+ */
267
+ function singleNumber (nums ) {
268
+ const set = new Set ();
269
+
270
+ for (const x of nums) {
271
+ if (set .has (x)) set .delete (x);
272
+ else set .add (x);
273
+ }
274
+
275
+ return [... set];
276
+ }
277
+ ```
278
+
279
+ <!-- tabs: end -->
280
+
281
+ <!-- solution: end -->
282
+
239
283
<!-- problem: end -->
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[] } nums
3
+ * @return {number[] }
4
+ */
5
+ function singleNumber ( nums ) {
6
+ const set = new Set ( ) ;
7
+
8
+ for ( const x of nums ) {
9
+ if ( set . has ( x ) ) set . delete ( x ) ;
10
+ else set . add ( x ) ;
11
+ }
12
+
13
+ return [ ...set ] ;
14
+ }
Original file line number Diff line number Diff line change
1
+ function singleNumber ( nums : number [ ] ) : number [ ] {
2
+ const set = new Set < number > ( ) ;
3
+
4
+ for ( const x of nums ) {
5
+ if ( set . has ( x ) ) set . delete ( x ) ;
6
+ else set . add ( x ) ;
7
+ }
8
+
9
+ return [ ...set ] ;
10
+ }
You can’t perform that action at this time.
0 commit comments