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;
               }
           }
        }
    };
    --------------------------------------------------------------------天道酬勤!
  • 相关阅读:
    Java对【JSON数据的解析】--Gson解析法
    Java对【JSON数据的解析】--官方解析法
    Java之JSON数据
    网络编程应用:基于UDP协议【实现聊天程序】--练习
    {网络编程}和{多线程}应用:基于UDP协议【实现多发送方发送数据到同一个接收者】--练习
    PHP获取页面执行时间的方法(推荐)
    Linux下查看CPU型号,内存大小,硬盘空间的命令(详解)
    Elasticsearch 全字段搜索_all,query_string查询,不进行分词
    mysql 查询某字段值全是数字
    linux服务器中Apache隐藏index.php失败
  • 原文地址:https://www.cnblogs.com/graph/p/3027586.html
Copyright © 2011-2022 走看看