| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | --- | 
					
						
							|  |  |  |  | id: a3f503de51cfab748ff001aa | 
					
						
							| 
									
										
										
										
											2021-10-18 08:17:43 -07:00
										 |  |  |  | title: 成对 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | challengeType: 5 | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  |  | forumTopicId: 301617 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  |  | dashedName: pairwise | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | --- | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  |  | # --description--
 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-18 08:17:43 -07:00
										 |  |  |  | 给定一个数组 `arr` ,找到其中总和等于第二个参数 `arg` 的元素对,并返回它们的索引之和。 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-18 08:17:43 -07:00
										 |  |  |  | 你可以使用具有相同数字元素但索引不同的多个对。 每对应使用尽可能低的索引。 一旦元素被使用,它就不能被重用来与另一个元素配对。 例如, `pairwise([1, 1, 2], 3)` 使用索引为 0 的 1,而不是索引为 1 的 1 来创建一对 `[2, 1]`,因为 0 + 2 < 1 + 2。 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-18 08:17:43 -07:00
										 |  |  |  | 例如, `pairwise([7, 9, 11, 13, 15], 20)` 返回 `6`。 总和为 20 的对是 `[7, 13]` 和 `[9, 11]`。 然后我们可以用它们的索引和值写出数组。 | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | <div style='margin-left: 2em;'> | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-18 08:17:43 -07:00
										 |  |  |  | | 索引 | 0 | 1 | 2  | 3  | 4  | | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  |  | | ----- | - | - | -- | -- | -- | | 
					
						
							| 
									
										
										
										
											2021-10-18 08:17:43 -07:00
										 |  |  |  | | 值 | 7 | 9 | 11 | 13 | 15 | | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | </div> | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-18 08:17:43 -07:00
										 |  |  |  | 接下来,我们将获取它们的相应索引并添加它们。 | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | <div style='margin-left: 2em;'> | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-18 08:17:43 -07:00
										 |  |  |  | 7 + 13 = 20 → 索引 0 + 3 = 3   | 
					
						
							|  |  |  |  | 9 + 11 = 20 → 索引 1 + 2 = 3   | 
					
						
							|  |  |  |  | 3 + 3 = 6 →返回 `6` | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | </div> | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  |  | # --hints--
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-18 08:17:43 -07:00
										 |  |  |  | `pairwise([1, 4, 2, 3, 0, 5], 7)` 应该返回 11。 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | ```js | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  |  | assert.deepEqual(pairwise([1, 4, 2, 3, 0, 5], 7), 11); | 
					
						
							|  |  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-18 08:17:43 -07:00
										 |  |  |  | `pairwise([1, 3, 2, 4], 4)` 应返回 1。 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  |  | ```js | 
					
						
							|  |  |  |  | assert.deepEqual(pairwise([1, 3, 2, 4], 4), 1); | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | ``` | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-18 08:17:43 -07:00
										 |  |  |  | `pairwise([1, 1, 1], 2)` 应该返回 1。 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  |  | ```js | 
					
						
							|  |  |  |  | assert.deepEqual(pairwise([1, 1, 1], 2), 1); | 
					
						
							|  |  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-18 08:17:43 -07:00
										 |  |  |  | `pairwise([0, 0, 0, 0, 1, 1], 1)` 应该返回10。 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  |  | ```js | 
					
						
							|  |  |  |  | assert.deepEqual(pairwise([0, 0, 0, 0, 1, 1], 1), 10); | 
					
						
							|  |  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-18 08:17:43 -07:00
										 |  |  |  | `pairwise([], 100)` 应该返回 0。 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | ```js | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  |  | assert.deepEqual(pairwise([], 100), 0); | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2020-08-13 17:24:35 +02:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  |  | # --seed--
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | ## --seed-contents--
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | ```js | 
					
						
							|  |  |  |  | function pairwise(arr, arg) { | 
					
						
							|  |  |  |  |   return arg; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | pairwise([1,4,2,3,0,5], 7); | 
					
						
							|  |  |  |  | ``` | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  |  | # --solutions--
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  |  | ```js | 
					
						
							|  |  |  |  | function pairwise(arr, arg) { | 
					
						
							|  |  |  |  |   var sum = 0; | 
					
						
							|  |  |  |  |   arr.forEach(function(e, i, a) { | 
					
						
							|  |  |  |  |     if (e != null) { | 
					
						
							|  |  |  |  |       var diff = arg-e; | 
					
						
							|  |  |  |  |       a[i] = null; | 
					
						
							|  |  |  |  |       var dix = a.indexOf(diff); | 
					
						
							|  |  |  |  |       if (dix !== -1) { | 
					
						
							|  |  |  |  |         sum += dix; | 
					
						
							|  |  |  |  |         sum += i; | 
					
						
							|  |  |  |  |         a[dix] = null; | 
					
						
							|  |  |  |  |       } | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  |   }); | 
					
						
							|  |  |  |  |   return sum; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | pairwise([1,4,2,3,0,5], 7); | 
					
						
							|  |  |  |  | ``` |