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

1.Two Sum

时间:2017-09-02 23:24:01      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:ted   bsp   order   twosum   tor   元素   not   col   example   

1.Two Sum

Given an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

题解一:

  采用哈希表。判断两个数之合是否是target------xi+xj=target=>xi=target-xj,我们可以将问题转化为找一个数是否在数组中。

代码:

 python

技术分享
 1 class Solution(object):
 2     def twoSum(self, nums, target):
 3         """
 4         :type nums: List[int]
 5         :type target: int
 6         :rtype: List[int]
 7         """
 8         hashdict={}
 9         
10         for i,item in enumerate(nums):
11             if(target-item) in hashdict:
12                 return (hashdict[target-item],i)
13             hashdict[item]=i
14         return []
View Code

 C++

技术分享
 1 class Solution {
 2 public:
 3     vector<int> twoSum(vector<int>& nums, int target) {
 4         vector<int>result;
 5         int length=nums.size();
 6         if(length<2)return result;
 7         //创建哈希表
 8         unordered_map<int,int>hash(length);
 9         
10         for(size_t i(0);i<length;i++){
11             if(hash.find(target-nums[i])!=hash.end()){
12                 result.push_back(hash[target-nums[i]]);
13                 result.push_back(i);
14                 return result;
15             }
16             else{
17                 hash[nums[i]]=i;
18             }
19         }
20         return result;
21     }
22 };
View Code

 

题解二  --- 排序后使用两个指针

先对数组进行排序,然后使用两个指针分别指向首尾元素,逐步向中间靠拢,直至找到满足条件的索引。

代码:

C++

技术分享
 1 class Solution {
 2 public:
 3     /*
 4      * @param numbers : An array of Integer
 5      * @param target : target = numbers[index1] + numbers[index2]
 6      * @return : [index1+1, index2+1] (index1 < index2)
 7      */
 8     vector<int> twoSum(vector<int> &nums, int target) {
 9         vector<int> result;
10         const int length = nums.size();
11         if (0 == length) {
12             return result;
13         }
14 
15         // first num, second is index
16         vector<pair<int, int> > num_index(length);
17         // map num value and index
18         for (int i = 0; i != length; ++i) {
19             num_index[i].first = nums[i];
20             num_index[i].second = i + 1;
21         }
22 
23         sort(num_index.begin(), num_index.end());
24         int start = 0, end = length - 1;
25         while (start < end) {
26             if (num_index[start].first + num_index[end].first > target) {
27                 --end;
28             } else if(num_index[start].first + num_index[end].first == target) {
29                 int min_index = min(num_index[start].second, num_index[end].second);
30                 int max_index = max(num_index[start].second, num_index[end].second);
31                 result.push_back(min_index);
32                 result.push_back(max_index);
33                 return result;
34             } else {
35                 ++start;
36             }
37         }
38 
39         return result;
40     }
41 };
View Code

 

源码分析

  1. 异常处理。
  2. 使用length保存数组的长度,避免反复调用nums.size()造成性能损失。
  3. 使用pair组合排序前的值和索引,避免排序后找不到原有索引信息。
  4. 使用标准库函数排序。

 

复杂度分析

遍历一次原数组得到pair类型的新数组,时间复杂度为 O(n),空间复杂度也为 O(n),标准库中的排序方法时间复杂度近似为 O(nlogn)两根指针遍历数组时间复杂度为 O(n)

 

1.Two Sum

标签:ted   bsp   order   twosum   tor   元素   not   col   example   

原文地址:http://www.cnblogs.com/ktao/p/7468107.html

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