codewars sum of pairs
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的更多相关文章
- Leetcode easy
1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a ...
- 九章lintcode作业题
1 - 从strStr谈面试技巧与代码风格 必做题: 13.字符串查找 要求:如题 思路:(自写AC)双重循环,内循环读完则成功 还可以用Rabin,KMP算法等 public int strStr( ...
- [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, ...
- Array Partition I
Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1 ...
- 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 ...
- leetcode算法题3:分组,让每个组的最小者,相加之后和最大。想知道桶排序是怎么样的吗?
/* Given an array of 2n integers, your task is to group these integers into n pairs of integer, say ...
- 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 ...
- [LeetCode] Array Partition I 数组分割之一
Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1 ...
- 561. Array Partition I
Given an array of 2n integers, your task is to group these integers into n pairs of integer, say \(( ...
随机推荐
- 基于开源串口调试助手修改的qcom
代码已上传码云: https://gitee.com/fensnote/qcom.git 源代码用于串口编程的学习很有价值,谢谢Qter的开源项目,感谢花心萝卜工作室的修改版本. 开源的qt开发的串口 ...
- 2020年最新的过某宝滑块验证技术,Python大牛轻松搞定技术难题
致谢: Charles未晞 Charles的皮卡丘 提供解决思路,此文实为借鉴. 前言: 利用selenium模拟登录淘宝,外界传言这个确实很难过,有各种轨迹检测. 但是我没想到淘宝的滑块验证码这么容 ...
- 基于索引的QA问答对匹配流程梳理
知识库(主要是标准的QA信息)匹配需求是对已经梳理出的大量标准QA对信息进行匹配,找出最符合用户问题的QA对进行回复,拆分主要的处理流程主要为如下两点: 标准QA信息入库索引: 通过对用户提出的问题进 ...
- Java—Map接口中的常用方法
Map接口与Collection接口的区别 Collection中的集合,元素是孤立存在的(理解为单身),向集合中存储元素采用一个个元素的方式存储. Map中的集合,元素是成对存在的(理解为夫妻).每 ...
- Android 开发学习进程0.15 adb cardview framelayout 控件设置状态获取焦点
Android设备调试桥 即adb 使用adb进行无线调试的一些常用命令 adb tcpip 5555 设置调试端口为5555 防止冲突 adb shell ifconfig wlan0 查询局域网中 ...
- yb课堂之压力测试工具Jmeter5.X 实战《二十二》
目前常用的测试工具对比 LoadRunner 性能稳定,压测结果及细粒度大,可以自定义脚本进行压力,但是太过于重大,功能比较繁多 Apache AB(单接口压测最方便) 模拟多线程并发请求,ab命令对 ...
- ls-remote -h -t git://github.com/adobe-webplatform/eve.git 报错问题
npm ERR! Error while executing:npm ERR! D:\开发工具\git\Git\cmd\git.EXE ls-remote -h -t git://github.com ...
- 解决VS Code安装golang相关插件失败问题
解决VS Code安装golang相关插件失败问题 VSCode作为一款优秀的编辑器,基本上前端开发,Python,markdown都可以用,真正是开发过程中的好帮手,所以学习go也当仁不让的要用vs ...
- Advances and Open Problems in Federated Learning
挖个大坑,等有空了再回来填.心心念念的大综述呀(吐血三升)! 郑重声明:原文参见标题,如有侵权,请联系作者,将会撤销发布! 项目地址:https://github.com/open-intellige ...
- IDEA常用快捷键Mac os和Windows对照--用到了就会更新
之前公司用了一段的MacBookPro,离职后自己入手了一台MacBookPro.但是现在的公司中使用的电脑是古老的win7,两个系统的键盘有些许差别,而且快捷键也略有不同.最近因为疫情影响,在家远程 ...