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. Linux系统之《消息队列》入手应用

    目录 简述 代码 编译 运行 简述 消息队列是Linux进程间通信方式之一,消息队列一般是用于简单的通信,数据量不大,通信不频繁的情况.如果交互频繁或者数据量大就不适合了. 代码 下面直接上代码,发送 ...

  2. 计算vtable的大小

    在ClassFileParser::parseClassFile()函数中会计算vtable和itable所需要的大小,因为vtable和itable是内嵌在Klass中的,parseClassFil ...

  3. 最新版CentOS8系统安装和基本配置

    一.Centos8新版简介 Redhat公司在2019年5月推出了rhel8,年底推出了centos8,紧接着也会把相关的技术认证完全转换为新平台. 阿里云.华为云目前也已推出centos8的公共镜像 ...

  4. 阿里P8架构师大话设计模式,体会乐与怒的程序人生中值得回味一幕

    本书特色 本书有两个特色,第一特色是重视过程.看了太多的计算机编程类的图书,大多数书籍都是集中在讲授优秀的解决方案或者一个完美的程序样例,但对这些解决方案和程序的演变过程却重视不够,好书之所以好,就是 ...

  5. android.content.ActivityNotFoundException: No Activity found to handle Intent { (has extras) }

    报错: 初始代码: @OnClick(R.id.include_top_iv_more) public void onViewClicked() { Intent intent_chat_set = ...

  6. 四则运算生成命令行程序 (Python)

    Github项目地址:Github Pages 结对项目成员:张鹏 3118004985 郑靓 3118004988 一.项目需求分析 二.功能实现 三.代码实现or功能说明 ★ GUI功能扩展说明 ...

  7. 牛客网PAT练兵场-D进制的A+B

    题解:大多数做法是利用循环相除,取余.我是将将A+B传入f函数,利用递归实现D进制的输出 题目地址:https://www.nowcoder.com/questionTerminal/a2063993 ...

  8. 区块链入门到实战(28)之Solidity – 介绍

    Solidity语言是一种面向合约的高级编程语言,用于在以太坊区块链网络上实现智能合约.Solidity语言深受c++.Python和JavaScript的影响,针对以太坊(Ethereum)虚拟机( ...

  9. python实现对列表的增删查修操作

    #定义一个空列表 list_demo=[] #1,向列表中插入元素 def append_demo(): #第一种使用append,可以在列表末尾添加一个函数 for i in range(2): l ...

  10. springboot-ssm的pom.xml

    springboot-ssm的pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&qu ...