# The Sum of

Source: Internet
Author: User

Solution 1:50ms

`Class solution {public:    vector<int> twosum (vector<int>&  nums, int target)  {        vector<int>  copy = nums;        quicksort (copy, 0,  Nums.size ()  - 1);        vector<int>::iterator  Index1 = copy.begin (),  index2 = copy.end ()  - 1;         while  ((*index1 + *index2 )  != target)          {            if   ((*INDEX1&NBSP;+&NBSP;*INDEX2)  < target)                  ++index1;             if  (*index1&NBSP;+&NBSP;*INDEX2)  > target)                  --index2;        }         int value1 = *index1, value2 = *index2;         int i1 = 0, i2 = 0;         for  (Index1 = nums.begin (); Index1 != nums.end (); + +INDEX1)         {             if  (*index1 == value1 | |  *index1 == value2)             {                 if  (!I1)             &nbsP;       i1 = index1 - nums.begin ()  + 1;                 else                 {                     i2  = index1 - nums.begin ()  + 1;                     break;                 }             }        }         vector<int> result;        result.push_back (I1) ;     &Nbsp;   result.push_back (I2);         return result ;     }    void quicksort (vector<int>& nums,  Int front, int behind)     {         int f = front + 1, b = behind, temp;         if  (Front >= behind)              return;        while  (F&NBSP;!=&NBSP;B)         {             if  (Nums[f] > nums[front])              {                 temp = nums[f];                 nums[f] = nums[b];                 nums[b] = temp;                 --b;            }             else                 ++f;         }        if  (Nums[f] > nums[front])             --f;         temp = nums[front];        nums[front]  = nums[f];   &nbSp;    nums[f] = temp;        quicksort ( nums, front, f - 1);         quicksort (nums, f  + 1, behind);    }};`

Solution 2:24ms

`Class solution { public:     vector<int> twosum (vector< Int>& nums, int target)  {         map <int, int> value_index;         vector<int>  result;         map<int, int>::iterator  iter;         int i = 0;          for  (I = 0;i < nums.size (); ++i)           {              iter = value_index.find (Target - nums[i]);              if  (Iter != value_index.end ())               {                  result.push_back (Iter->second);                  result.push_back (i + 1);                  break;              }              else                  value_index.insert (pair<int, int> (nums[i], i + 1));          }          return result;     } };`

Solution 3:12ms

`Class solution { public:     vector<int> twosum (vector< Int>& nums, int target)  {          vector<int> copy = nums;         vector< Int> result;         sort (Copy.begin (),  copy.end ());          int f = 0, b = copy.size ()  - 1;         while  ((Copy[f] + copy[b])  != target)          {              if  ((Copy[f] + copy[b])  < target)              {                  ++f;              }             if   ((copy[f] + copy[b] > target))               {                  --b;             }          }          int i = 0, index1 = 0, index2 = 0;          for  (I = 0;i < nums.size (); ++i)           {              if  ((nums[i] ==  copy[f])  | | &NBSP;NUMS[I]&NBSP;==&NBSP;COPY[B])               {                  if  (!INDEX1)                   {                         index1 = i + 1;                  }                  else                  {                      index2 = i  + 1;                      break;                  }             }          }          Result.push_back (INDEX1);          result.push_back (INDEX2);          return result;     } };`

The Sum of

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.