码迷,mamicode.com
首页 > 其他好文 > 详细

Two Sum

时间:2015-11-20 23:11:30      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:

 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));

 

Two Sum

标签:

原文地址:http://www.cnblogs.com/huoteng/p/4982459.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!