LeetCode_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

  解法一:暴力法

vector<int> twoSum(vector<int> &numbers, int target) {

        // Start typing your C/C++ solution below

        // DO NOT write int main() function

        int i,j;

        bool flag = true;

        for( i = 0; i<numbers.size()-1 && flag;i++)

          for(j = i+1 ; j< numbers.size()&&flag ;j++)

             if( numbers[i] + numbers[j] == target)

                 flag = false ;

             

        vector<int> result;

        result.push_back(i);

        result.push_back(j);

        

        return result;

    }

解法二: 排序+ 二分+ 夹逼(尚未测试通过)

class Solution {

public:

   

   struct Item{

   int value;

   int ID;

   Item(int a,int b)

   {

     value = a;

     ID = b ;

   }

   bool operator <(const Item& tp) const

   {

     return value < tp.value;

   }

};

    vector<int> twoSum(vector<int> &numbers, int target) {

        // Start typing your C/C++ solution below

        // DO NOT write int main() function

        int i,j;

        vector<struct Item> temp;

        for( i = 0; i<numbers.size();i++)

        {

           Item  mystrc(numbers[i],i+1);

           temp.push_back(mystrc);           

              

        }

        sort(temp.begin(), temp.end());

        i = 0;

        j = numbers.size() -1;

        int mid;

        while(i<j)

        {

           mid = (i+j)>>2;

           if(temp[mid].value > target)

               j = mid -1;

            else   if(temp[mid].value < target)

               i = mid =1;

               else

                  break;

        }

        i = 0;

        j = mid;

        while(i <mid)

        {

          int sum = temp[i].val + temp[j].val ;

          if(sum < target)

                 i++;

           else if(sum > target)

              j--;

              else

                  break;

        }

       

        vector<int> result;

        result.push_back(temp[i].Id);

        result.push_back(temp[j].Id);

        

        return result;

    }

};

 

 解法三: 使用hash

class Solution {

public:

    vector<int> twoSum(vector<int> &numbers, int target) {

        // Note: The Solution object is instantiated only once and is reused by each test case.

       int len = numbers.size();

       map<int,int> mymap;

       vector<int> res;

       

       for(int i = 0; i < len; ++i){

               mymap[numbers[i]] =i;

       }

       for(int i = 0; i < len;++i){

           int tp = target - numbers[i];

           auto it = mymap.find(tp);

           if(it != mymap.end()){

               res.push_back(i+1);

               res.push_back((it->second) +1);

               return res;

           }

       }

    }

};

 

 

你可能感兴趣的:(LeetCode)