问题描述:

给定一个整数数列,找出其中和为特定值的那两个数。

你可以假设每个输入都只会有一种答案,同样的元素不能被重用。

示例:

给定 nums = [2, 7, 11, 15], target = 9

因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]

我的答案:

 /**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function(nums, target) {
var a=[];
for(var i=0;i<nums.length-1;i++){
for(var j=i+1;j<nums.length;j++){
if(nums[i]+nums[j]==target){
a.push(i);
a.push(j);
}
}
}
return a;
};

优秀答案:

参考 http://www.cnblogs.com/kiznaiver1998/p/8605809.html

解题思路:构造了arr{ key:value} 对象。将target-nums[i] 差值放在arr{ }对象中,并存储其位置i。然后每次就在arr{ }对象中找有没有对应的数即可。

function twoSum(nums, target) {
var arr = {};
for (var i = 0; i < nums.length; i++) {
if (typeof(arr[nums[i]]) !== "undefined"){
return [arr[nums[i]], i];
}
arr[target - nums[i]] = i;
}
}

leetcode--js--Two Sum的更多相关文章

  1. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  2. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  3. [leetCode][013] Two Sum 2

    题目: Given an array of integers that is already sorted in ascending order, find two numbers such that ...

  4. [LeetCode] #167# Two Sum II : 数组/二分查找/双指针

    一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...

  5. [LeetCode] #1# Two Sum : 数组/哈希表/二分查找/双指针

    一. 题目 1. Two SumTotal Accepted: 241484 Total Submissions: 1005339 Difficulty: Easy Given an array of ...

  6. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

  7. [array] leetcode - 39. Combination Sum - Medium

    leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...

  8. LeetCode one Two Sum

    LeetCode one Two Sum (JAVA) 简介:给定一个数组和目标值,寻找数组中符合求和条件的两个数. 问题详解: 给定一个数据类型为int的数组,一个数据类型为int的目标值targe ...

  9. [leetcode]40. Combination Sum II组合之和之二

    Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...

  10. [LeetCode] 437. Path Sum III_ Easy tag: DFS

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

随机推荐

  1. 使用Oracle Stream Analytics 21步搭建大数据实时流分析平台

    概要: Oracle Stream Analytics(OSA)是企业级大数据流实时分析计算平台.它可以通过使用复杂的关联模式,扩充和机器学习算法来自动处理和分析大规模实时信息.流式传输的大数据可以源 ...

  2. HGE引擎改进——2014/3/4

    2014/3/4 更新 1.提升资源包管理效率 2.Show库整合.目前Show库有Picture.Frame.Animation和Particle类,以及PictureData和ParticleSy ...

  3. JDBC的学习笔记-手动实现

    JDBC是SUN公司提供的一套用于数据库操作的接口,Java程序员只需要面向这套接口编程即可.不同的数据库厂商,需要针对这套接口,提供不同实现. 使用JDBC的好处:1.程序员不需要关注不同数据库的细 ...

  4. Unicode编码解析

    概述 转载自博文浅谈Unicode编码 参考博文,结合自己实际情况进行部分内容添加修改 一 需求 java源码中尤其是数字部分用到了很多Unicode方面知识,如果不懂,看源码的时候会很懵逼 java ...

  5. Linux防火墙之iptables扩展处理动作

    前文我们讲了iptables的扩展匹配,一些常用的扩展模块以及它的专有选项的使用和说明,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/12285152.html ...

  6. python day01学习

    1.python语言 # 89年 龟叔2.python的特点 # 优点 : 简明 简单 跨平台性好 # 缺点 : 慢 -执行速度相对其他语言慢 # 编程语言的分类:  # 编译型语言: c c++ j ...

  7. rep stos 指令(Intel汇编)

    今天读代码时,忽然跳出如下一条指令==>> 汇编代码: rep stos dword ptr es:[edi] 在网上查了相关资料显示: /************************ ...

  8. @ComponentScan注解,basePackages参数通配符

    @ComponentScan(basePackages = "com.ofo.test")当basePackages的直使用通配符,使用**,不能使用*.引用:https://bl ...

  9. 小白学 Python 数据分析(3):Pandas (二)数据结构 Series

    在家为国家做贡献太无聊,不如跟我一起学点 Python 顺便问一下,你们都喜欢什么什么样的文章封面图,老用这一张感觉有点丑 人生苦短,我用 Python 前文传送门: 小白学 Python 数据分析( ...

  10. Go语言实现:【剑指offer】二叉搜索树的第k个的结点

    该题目来源于牛客网<剑指offer>专题. 给定一棵二叉搜索树,请找出其中的第k小的结点.例如,(5,3,7,2,4,6,8) 中,按结点数值大小顺序第三小结点的值为4. Go语言实现: ...