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. Android html5和Android之间的交互

    今天补充了会昨天的问题,然后搞半天又出现莫名其妙的问题. 今天讲的是交互,先说html5在Android的调用. 上面的hello world上面的部分都是安卓里的布局 然后按这些布局自动生成代码. ...

  2. nginx多个server的配置,同一端口

    nginx多个server的配置,同一端口 #user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/e ...

  3. 022_go语言中的协程

    代码演示 package main import "fmt" func f(from string) { for i := 0; i < 3; i++ { fmt.Print ...

  4. 介绍一种 Python 更方便的爬虫代理池实现方案

    现在搞爬虫,代理是不可或缺的资源 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经做案例的人,却不知道如何去学习更加高深的知识.那 ...

  5. 使用Luhn算法实现信用卡号验证

    问题描述: 2:信用卡号的验证 [信用卡号的验证] 当你输入信用卡号码的时候,有没有担心输错了而造成损失呢?其实可以不必这么 担心,因为并不是一个随便的信用卡号码都是合法的,它必须通过 Luhn 算法 ...

  6. 学习Hibernate5 JPA这一篇就够了

    配套资料,免费下载 链接:https://pan.baidu.com/s/158JkczSacADez-fEMDXSYQ 提取码:2ce1 复制这段内容后打开百度网盘手机App,操作更方便哦 第一章 ...

  7. 定宽整形(C++11起)

    定义于头文件 cstdint中int8_t.int16_t.int32_t.int64_t 分别为宽度恰为 8 . 16 . 32 和 64 位的有符号整数类型无填充位并对负值使用补码(仅若实现支持该 ...

  8. C#设计模式之19-观察者模式

    观察者模式(Observer Pattern) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/423 访问. 观察者模式 ...

  9. C#LeetCode刷题之#628-三个数的最大乘积( Maximum Product of Three Numbers)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3726 访问. 给定一个整型数组,在数组中找出由三个数组成的最大乘 ...

  10. 编写有提示的listbox控件 2008-06-29 17:13

    在MFC中几乎所有的控件都有信息提示,而惟有listbox却没有这样的一个功能,每当我们把鼠标移到listbox上控件时,啥玩意儿都没有是不是很气馁啊,所以我今天特地写了一个简单的有提示的listbo ...