问题描述:

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

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

示例:

给定 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. 洛谷P3645 [APIO2015]雅加达的摩天楼

    题目描述 印尼首都雅加达市有 N 座摩天楼,它们排列成一条直线,我们从左到右依次将它们编号为 0 到 N − 1.除了这 NN 座摩天楼外,雅加达市没有其他摩天楼. 有 M 只叫做 “doge” 的神 ...

  2. centOS7.1安装nginx与可能遇见的问题

    一,安装nginx(系统:CentOS7.1) 1.Nginx下载地址:http://nginx.org/download/nginx-1.6.2.tar.gz [root@linux src]# c ...

  3. EsClientRHL-elasticsearch java客户端开源工具

    EsClientRHL是一个可基于springboot的elasticsearch 客户端调用封装工具,通过elasticsearch官网推荐的RestHighLevelClient实现,内置了es索 ...

  4. Qt Installer Framework翻译(7-2)

    包文件夹 安装程序包含的组件,要么是内嵌的,要么可以从远程存储库加载.在这两种情况下,都需要为组件使用一种安装程序可以读取的文件格式和结构. 包文件夹结构 将所有组件放在相同的根文件夹中,即包文件夹. ...

  5. xsd 和 wsdl

    xsd : 可用方便 不同的语言之间的 用命令行来 转换对应语言的. wsdl: 可用方便不同语言的类描述 用命令行 来相互转换. 类似 thift me ?

  6. ios---二维码的扫描

    二维码扫描 使用ios的AVFoundation框架实现二维码扫描 第一步:设置相机访问权限:在Info.plist添加Privacy - Camera Usage Description权限 第二步 ...

  7. ES6笔记分享 part 2

    ECMAScript ES6 从一脸懵逼到灵活运用 接 part 1 New String Methods const id = 'adcd123456x'; const fan = 'I love ...

  8. ElasticSearch入门篇Ⅰ --- ES核心知识概括

    C01.什么是Elasticsearch 1.什么是搜索 垂直搜索(站内搜索) 互联网的搜索:电商网站,招聘网站,各种app IT系统的搜索:OA软件,办公自动化软件,会议管理,员工管理,后台管理系 ...

  9. react 获取input的值 ref 和 this.setState({})

    1.ref //class my_filter(reg){          const inpVal = this.input.value;          console.log(inpVal) ...

  10. python写的用WMI检测windows系统信息的脚本

    脚本如下: #!/usr/bin/env python #coding:utf- import wmi import sys,time,platform def get_system_info(os) ...