zoukankan      html  css  js  c++  java
  • 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;
               }
           }
        }
    };
    --------------------------------------------------------------------天道酬勤!
  • 相关阅读:
    立波育儿百科,在手机上的育儿百科使您随时随地查看别人的育儿心得、专家的精彩讲解,使怀孕、养育孩子成为了一种乐趣
    把表单上的数据清除就可以了
    忽略特殊文件
    git/github运用
    Git中三种文件状态及其转换
    git取消文件跟踪
    学习 JSF 2.0 新链接、RefCards、样例、JSF Fu...
    JBoss Seam 的前景
    爆笑囧人囧事 2009 大合集!
    学习 JSF 2.0 新链接、RefCards、样例、JSF Fu...
  • 原文地址:https://www.cnblogs.com/graph/p/3027586.html
Copyright © 2011-2022 走看看