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基础概念介绍,对基本概念了解后,更好入门. 这里主要介绍一下几个概念 什么是linux GNU项目和自由软件基金会 linux发行版 什么是linux   也许大家都已经知道,L ...

  2. Ubuntu定时执行任务(定时爬取数据)

    cron是一个Linux下的后台进程,用来定期的执行一些任务.因为我用的是Ubuntu,所以这篇文章中的所有命令也只能保证在Ubuntu下有效. 1:编辑crontab文件,用来存放你要执行的命令 s ...

  3. MyBatisPlus分页查询,删除操作

    分页查询 分页查询在网页使用十分之多 原始的limit进行分页 pageHelper第三方插件 3. MP内置的分页插件 导入配置 如何使用,官网的代码如下 //分页插件 @Bean public P ...

  4. JAVA多线程之生产者 消费者模式 妈妈做面包案例

    创建四个类 1.面包类 锅里只可以放10个面包 ---装面包的容器2.厨房 kitchen 生产面包 和消费面包  最多生产100个面包3.生产者4消费者5.测试类 多线程经典案例 import ja ...

  5. C#LeetCode刷题之#383-赎金信(Ransom Note)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3937 访问. 给定一个赎金信 (ransom) 字符串和一个杂志 ...

  6. Vue 使用$createElement自定义文本

    有时候弹窗或者一些特殊的区域需要自定义一些html元素来显示(snabbdom) const h = this.$createElement this.$msgbox({ title: '提示', m ...

  7. vue cli 中关于vue.config.js中chainWebpack的配置

    Vue CLI  的官方文档上写:调整webpack配置最简单的方式就是在vue.config.js中的configureWebpack选项提供一个对象. Vue CLI 内部的 webpack 配置 ...

  8. unity探索者之复制内容到剪贴板

    版权声明:本文为原创文章,转载请声明http://www.cnblogs.com/unityExplorer/p/8417014.html unity中提供的TextEditor类,只能在window ...

  9. win10中搭建Linux子系统

    win10自带的Linux子系统,简称WSL(Windows Subsystem for Linux).优点是打通了Linux系统和windows系统,改变了传统虚拟机/双系统造成的两个系统相互隔绝的 ...

  10. 一文读懂BeanFactory和FactoryBean区别

    一直以来,很多人对于Spring中的BeanFactory和FactoryBean都是分不清楚的 BeanFactory 这个其实是所有Spring Bean的容器根接口,给Spring 的容器定义一 ...