Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
..
Failed to load latest commit information.
README.md
c.cpp
j.js

README.md

title date tags
说明
2017-04-07 10:11:49 -0700
JavaScript

说明

方案1:

迭代*2,使第一个和第二个数相加,对比目的数

var len = nums.length;
for(var a = 0;a < len;a++){
    for(var b = a + 1;b < len;b++){
        if(nums[a] + nums[b] == target){
            return [a, b];
        }
    }
}

方案2:

迭代,使用目的数除去第一个数,将结果(用于与第二个数对比)作为键名保存入一个数组,判断键名是否与当前迭代数相等

var t;
var n = [];
for(var a = 0;a < nums.length;a++){
    t = target - nums[a];
    if(n[t] !== undefined){
        return [n[t], a];
    }
    n[nums[a]] = a;
}