标签:
1 var twoSum = function(nums, target) {
2 var len = nums.length,
3 i = 0,
4 hash = {},
5 res = [],
6 t1, t2;
7
8 while (i < len) {
9 t1 = target - nums[i];
10 t2 = nums[i];
11 if (hash[t1] != undefined) {
12 res.push(hash[t1]);
13 res.push(i + 1);
14 break;
15 }
16 if (hash[t2] == undefined) {
17 hash[t2] = i + 1;
18 }
19 i++;
20 }
21
22 return res;
23 };
24
25 var nums = [2, 7, 11, 15];
26 console.log(twoSum(nums, 9));
标签:
原文地址:http://www.cnblogs.com/HuoAA/p/5074220.html