问题描述:

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

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

示例:

  1. 给定 nums = [2, 7, 11, 15], target = 9
  2.  
  3. 因为 nums[0] + nums[1] = 2 + 7 = 9
  4. 所以返回 [0, 1]

我的答案:

  1. /**
  2. * @param {number[]} nums
  3. * @param {number} target
  4. * @return {number[]}
  5. */
  6. var twoSum = function(nums, target) {
  7. var a=[];
  8. for(var i=0;i<nums.length-1;i++){
  9. for(var j=i+1;j<nums.length;j++){
  10. if(nums[i]+nums[j]==target){
  11. a.push(i);
  12. a.push(j);
  13. }
  14. }
  15. }
  16. return a;
  17. };

优秀答案:

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

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

  1. function twoSum(nums, target) {
  2. var arr = {};
  3. for (var i = 0; i < nums.length; i++) {
  4. if (typeof(arr[nums[i]]) !== "undefined"){
  5. return [arr[nums[i]], i];
  6. }
  7. arr[target - nums[i]] = i;
  8. }
  9. }

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. python super()函数:调用父类的构造方法

    python子类会继承父类所有的类属性和类方法.严格来说,类的构造方法其实就是实例方法,因此,父类的构造方法,子类同样会继承. 我们知道,python是一门支持多继承的面向对象编程语言,如果子类继承的 ...

  2. set去重

    public static void main(String[] args){ List<String> list = new ArrayList<String>(); lis ...

  3. python GUI测试自动化

    #! /usr/bin/env python#coding=GB18030'''GUI测试自动化 语言:python模块:pywinauto环境:windows7中文.python-2.6_32bit ...

  4. Java学习笔记(二) 面向对象---构造函数

    面向对象---构造函数 特点 函数名与类名相同 不用定义返回值类型 不写return语句 作用 对象一建立,就对象进行初始化. 具体使用情况 class Student { Student(){ Sy ...

  5. Dart语言学习(十二) Dart面向对象

    Dart作为一种高级语言,支持面向对象的很多特性,并且支持基于mixin的继承方式. 基于mixin的继承方式是指:一个类可以继承自多个父类,相当于其他语言里的多继承. 所有的类都有同一个基类Obje ...

  6. 脚本在Shell可以执行成功,放到crontab里执行失败

    一.背景 自己写了个监控MGR状态的脚本,直接在Linux的Shell环境下可以执行成功,但是只要放到crontab里执行,就失败,脚本内容如下 #!/bin/bash MAIL_ADDR=`cat ...

  7. nginx命令行及演示:重载、热部署、日志切割

    重载配置文件 nginx -s reload 热部署(升级nginx) 首先备份二进制文件 cp nginx nginx.old  拷贝新版本的nginx替换以前的nginx二进制文件 cp  ngi ...

  8. springmvc中applicationapplicationContext头部代码

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...

  9. Android View如何获取焦点

    Android新启动Activity,dialog或者其他窗体中中包含EditText, 新启动的activity的焦点默认在EditText上,这是android系统会弹出软键盘,挤压activit ...

  10. Kafka常用topic操作命令汇总

    offset topic consumer-group consumer producer producer-golang topic 工具 https://cwiki.apache.org/conf ...