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;
               }
           }
        }
    };
    --------------------------------------------------------------------天道酬勤!
  • 相关阅读:
    类的成员函数实现线程的回调函数
    Devexpress Chart series 点击时获取SeriesPoint的值
    递归树 TreeList
    ChartControl饼状图自定义调色板
    Devexpress GridControl.Export 导出
    .Net Core 实现 自定义Http的Range输出实现断点续传或者分段下载
    Js/Jquery获取网页屏幕可见区域高度
    js获取网页屏幕可视区域高度
    环境变量
    bat批处理文件怎么将路径添加到path环境变量中
  • 原文地址:https://www.cnblogs.com/graph/p/3027586.html
Copyright © 2011-2022 走看看