Given an array of integers nums  And a target value target, Please find the two integers in the array whose sum is the target value , And return their array subscripts .

notes ： You can assume that there is only one answer for each input . however , You can't reuse the same elements in this array .

Examples :

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

because nums[0] + nums[1] = 2 + 7 = 9
So go back [0, 1]

Solution 1 （C The law of violence ）：

notes ： Execution time 196ms, Memory consumption 7.5M
int* twoSum(int* nums, int numsSize, int target, int* returnSize)
//numsSize Is the number of array elements {     *returnSize = 2;  // The number of returned values is 2     int i,j;     int*
result = NULL;     for(i = 0; i < numsSize-1; ++i)     {         for(j = i+1; j
< numsSize; ++j)         {             if(nums[i] + nums[j] == target)
{                 result = (int*)malloc(sizeof(int)*2);
// Space for shaping pointer , It is equivalent to an integer array                 result[0] = i;                 result[1]
= j;                 return result; // Find the two integers whose sum is the target value , Returns their array subscripts             }
}     }     return 0; }

Solution 2 （C++ The law of violence ）：

notes ： Execution time 212ms, Memory consumption 9.2M
class Solution  { public:     vector<int> twoSum(vector<int>& nums, int
target)      {         int n = nums.size();         vector<int> result;
for(int i = 0; i < n-1; ++i)         {             for(int j = i+1; j < n; ++j)
{                 if(nums[i]+nums[j] == target)                 {
result.push_back(i);                     result.push_back(j);
return result;  // Find the two integers whose sum is the target value , Returns their array subscripts                 }
}         }         return result;     } };

Solution 3 （C++ Two times hash table ）：

A simple implementation uses two iterations . In the first iteration , We add the value of each element and its index to the table . then , In the second iteration , We will examine the target element for each element （target
- nums[i]target−nums[i]） Does it exist in the table . be careful , The target element cannot be nums[i]nums[i] itself !

notes ： Execution time 16ms, Memory consumption 10.5M
class Solution  { public:     vector<int> twoSum(vector<int>& nums, int
target)      {         int n = nums.size();         map<int,int>
array;  // establish hash Table stores array elements         vector<int> result(2,-1);  // Storage results , Initialize to two -1
for(int i = 0; i < n; ++i)         {             int x = target - nums[i];
if(array.count(x) > 0)             {                 result[0] =
array[x];                 result[1] = i;                 break;             }
array[nums[i]] = i;         }         return result;
// Find the two integers whose sum is the target value , Returns their array subscripts     } };

Solution 4 （C++ Hash table ）：

notes ： Execution time 12ms, Memory consumption 10.3M
class Solution  { public:     vector<int> twoSum(vector<int>& nums, int
target)      {         int n = nums.size();         map<int,int>
array;  // establish hash Table stores array elements         vector<int> result(2,-1);  // Storage results , Initialize to two -1
for(int i = 0; i < n; ++i)         {             int x = target - nums[i];
if(array.count(x) > 0)             {                 result[0] =
array[x];                 result[1] = i;                 break;             }
array[nums[i]] = i;         }         return
result;  // Find the two integers whose sum is the target value , Returns their array subscripts     } };

Technology
Daily Recommendation
views 5
views 2