Sum of Pairs

Given a list of integers and a single sum value, return the first two values (parse from the left please) in order of appearance that add up to form the sum.

sum_pairs([11, 3, 7, 5],         10)
# ^--^ 3 + 7 = 10
== [3, 7] sum_pairs([4, 3, 2, 3, 4], 6)
# ^-----^ 4 + 2 = 6, indices: 0, 2 *
# ^-----^ 3 + 3 = 6, indices: 1, 3
# ^-----^ 2 + 4 = 6, indices: 2, 4
# * entire pair is earlier, and therefore is the correct answer
== [4, 2] sum_pairs([0, 0, -2, 3], 2)
# there are no pairs of values that can be added to produce 2.
== None/nil/undefined (Based on the language) sum_pairs([10, 5, 2, 3, 7, 5], 10)
# ^-----------^ 5 + 5 = 10, indices: 1, 5
# ^--^ 3 + 7 = 10, indices: 3, 4 *
# * entire pair is earlier, and therefore is the correct answer
== [3, 7]

Negative numbers and duplicate numbers can and will appear.

NOTE: There will also be lists tested of lengths upwards of 10,000,000 elements. Be sure your code doesn't time out.

这道题目的意思是说:给出一个数组,然后给出一个number,需要从数组中找到两个元素的和等于目标值得number,如果有多对组合的,需要对多对组合进行下标比对,下标整体靠前的就是目标选项,将该组合返回即可,如果没有找到,返回undefined。

特别注意的是,它的测试中会有一个特别长的数组,长度可能会超过10,000,000个元素,如果你的算法不够高效,很有可能会超时,它设置的超时时间是12s。

我思考了半天,第一个办法是按照常规,双重for循环找到所有组合,然后再对找到的所有组合比对下标,整体靠前的返回。

没错,你想到了,方法没错,返回超时。

方法二:这回学聪明了,先找到一组组合,然后再循环时从比它下标小的范围内寻找,如果没有找到,就把第一组组合返回,如果找到了就替换,然后继续在更小的范围内寻找。最坏的情况是都找完,找不到的话,返回undefined。

以为优化了不少了,但是依然timeout。

/your code here
let result = [];
let pos = 0;
for (let i = 0; i < ints.length - 1; i++) {
if (result[1]) {
if(i>pos){
break;
}
for (let j = i + 1; j < pos; j++) {
if (ints[i] + ints[j] == s) {
result = [ints[i], ints[j]];
}
}
} else {
for (let j = i + 1; j < ints.length; j++) {
if (ints[i] + ints[j] == s) {
if (!result[1] || (result[1] && result[1] > j)) {
result = [ints[i], ints[j]];
pos = j;
}
}
}
}
}
return result.length > 0 ? result : undefined;

没错,我就是这么菜,水平到这里了,江郎才尽了。。。

不过别失望,带你去看看大神的代码吧,吸收一点经验也是好的。

大神代码:

var sum_pairs=function(ints, s){
var seen = {}
for (var i = 0; i < ints.length; ++i) {
if (seen[s - ints[i]]) return [s - ints[i], ints[i]];
seen[ints[i]] = true
}
}

什么,代码居然可以这么简练?没错,就是这么简练,然后细细品味一番,它巧妙的把对比过的数字变成一个对象的属性并且设置为true,然后用seen(s-ints[i])是否为true来判定是否找到了组合,一旦找到了,它们的下标必定是最小的。因为该方法的重点就是找到最小的下标值,控制好了最小下标值,你就赢了!!!

codewars sum of pairs的更多相关文章

  1. Leetcode easy

    1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a ...

  2. 九章lintcode作业题

    1 - 从strStr谈面试技巧与代码风格 必做题: 13.字符串查找 要求:如题 思路:(自写AC)双重循环,内循环读完则成功 还可以用Rabin,KMP算法等 public int strStr( ...

  3. [leetcode-561-Array Partition I]

    Given an array of 2n integers, your task is to group these integers into n pairs of integer,say (a1, ...

  4. Array Partition I

    Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1 ...

  5. LeetCode 561. Array Partition I (数组分隔之一)

    Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1 ...

  6. leetcode算法题3:分组,让每个组的最小者,相加之后和最大。想知道桶排序是怎么样的吗?

    /* Given an array of 2n integers, your task is to group these integers into n pairs of integer, say ...

  7. leetcode 561.Array Partition I-easy

    Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1 ...

  8. [LeetCode] Array Partition I 数组分割之一

    Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1 ...

  9. 561. Array Partition I

    Given an array of 2n integers, your task is to group these integers into n pairs of integer, say \(( ...

随机推荐

  1. 基于视频压缩的实时监控系统-sprint3采集端传输子系统设计

    由于jpg本来就是编码压缩后的格式,所有无需重复编码 传输子系统步骤:(1)初始化:a.socket(初始化tcp连接):b.将事件添加到epoll中 (2)事件处理:接收到网络包.发送完网络包 st ...

  2. CF1349F 【Slime and Sequences】part1

    由于本文过长,\(\LaTeX\) 炸了,分两篇,part2 题目描述 定义一个正整数序列为好序列,当且仅当如果某个数 \(k\) 出现过,那么一定有 \(k-1\) 在最后一个 \(k\) 的前面出 ...

  3. Python使用Tornado+Redis维护ADSL拨号服务器代理池

    们尝试维护过一个免费的代理池,但是代理池效果用过就知道了,毕竟里面有大量免费代理,虽然这些代理是可用的,但是既然我们能刷到这个免费代理,别人也能呀,所以就导致这个代理同时被很多人使用来抓取网站,所以当 ...

  4. 使用HttpClient 发送 GET、POST(FormData、Raw)、PUT、Delete请求及文件上传

    httpclient4.3.6 package org.caeit.cloud.dev.util; import java.io.File; import java.io.IOException; i ...

  5. Redis服务之Redis Cluster

    在上一篇博客中我们聊到了redis的高可用组件sentinel的相关配置,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/13429776.html:sentin ...

  6. 线程通讯wait&notify

    目录 相关概念 生产者&消费者模型 相关概念 锁:解决线程间冲突的问题 wait&notify:解决线程间协作的问题 wait和sleep的区别 wait期间对象锁是释放的,而slee ...

  7. Flutter 容器(3) - AnimatedPadding

    AnimatedPadding : 会产生动画效果的padding,在给定时间内缩放到指定padding import 'package:flutter/material.dart'; class A ...

  8. 01从DataGrid中导入到Excel

    01网络上有很多导出数据到Excel的方法,我在网上找到了一种比较简单实用的方法(参考了网友的方法) string fileName = ""; Microsoft.Win32.S ...

  9. node mssql 无法连接sql server

    mssql无法连接sql server主要有两种原因: Sql server使用的是Windows身份验证 Sql server并没有打开网络连接功能 1.打开Sql Server身份验证 参考这篇文 ...

  10. Robot Framework(8)——脚本语法示例记录

    大神写了一个Robot Framework的脚本,好多语法之前没接触过,就有了这篇,记录下来一起学习,欢迎纠错 第二三四五列,一般是入参,红色的表示必填的入参.浅灰色表示选填的入参.深灰色表示无需填写 ...