# Two Sum

Given an array of integers, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

You may assume that each input would have exactly one solution.

Input: numbers={2, 7, 11, 15}, target=9

Output: index1=1, index2=2

class Solution {
public:
template <typename T>
vector<size_t> sort_indexes(vector<T> &v) {

// initialize original index locations
vector<size_t> idx(v.size());
for (size_t i = 0; i != idx.size(); ++i) idx[i] = i;

// sort indexes based on comparing values in v
sort(idx.begin(), idx.end(),
[&v](size_t i1, size_t i2) {return v[i1] < v[i2];});
sort(v.begin(), v.end());
return idx;
}
int select(vector<int>& nums, int start, int end, int value)
{
if(start > end)
return -1;
if(nums[(start + end)/2] == value)
return (start + end)/2;
else if(nums[(start + end)/2] < value)
{
start = (start + end)/2 + 1;
select(nums, start, end, value);
}
else{
end = (start + end)/2 -1;
select(nums, start, end, value);
}
}
vector<int> twoSum(vector<int>& nums, int target) {
//sorted nums ascendingly.
vector<size_t> sorted_indexes = sort_indexes(nums);

int result;
vector<int> two_sum;
for(int i = 0; i < nums.size(); i++)
{
//look for number = target-nums[i]
int index = select(nums, i+1, nums.size() - 1, target-nums[i]);
if(index != -1)
{
int a1 = 0;
int a2 = 0;

if(sorted_indexes[i] < sorted_indexes[index]){
a1 = sorted_indexes[i]+1;
a2 = sorted_indexes[index] + 1;
}
else{
a1 = sorted_indexes[index] + 1;
a2 = sorted_indexes[i]+1;
}
two_sum.push_back(a1);
two_sum.push_back(a2);
return two_sum;
}
}
return two_sum;
}
};

• 本文已收录于以下专栏：

## LeetCode::Two Sum C语言

• u013300875
• 2015年03月07日 15:39
• 5513

## leetcode 刷题题解（c++） 1.Two Sum （hash表，排序+二分查找）

c++刷leetcode，有题目和算法代码，可直接在leetcode提交通过。
• GOGO_YAO
• 2016年07月24日 20:59
• 648

## 算法系列--Two Sum

• ylyg050518
• 2015年09月13日 23:34
• 928

## Two Sum -- LeetCode

• linhuanmars
• 2014年02月22日 23:11
• 20487

## LeetCode 1 Two Sum 题解

Problem： Given an array of integers, find two numbers such that they add up to a specific target nu...
• runningtortoises
• 2015年05月05日 16:53
• 1249

## [LeetCode]1 Two Sum(C++,Python实现)

LeetCode OJ的第一题，题目描述如下：
• Shiroh_ms08
• 2014年05月16日 21:17
• 1864

## leetcode 170: Two Sum III - Data structure design

Two Sum III - Data structure design Total Accepted: 311 Total Submissions: 1345 Design and implem...
• xudli
• 2014年12月31日 08:17
• 6981

## LeetCode || Two Sum

Two Sum  Total Accepted: 16363 Total Submissions: 87273My Submissions Given an array of ...
• 2014年04月09日 23:11
• 33862

## leetcode 1. Two Sum -----java

• j754379117
• 2016年06月20日 19:52
• 1404

## [LeetCode][1]Two Sum解析 -Java实现

Q: Given an array of integers, return indices of the two numbers such that they add up to a spe...
• u014629433
• 2016年06月01日 16:23
• 830

举报原因： 您举报文章：Two Sum 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)