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;
               }
           }
        }
    };
    --------------------------------------------------------------------天道酬勤!
  • 相关阅读:
    jQuery学习之过滤选择器
    ASP.net WebAPI 上传图片
    Mono for Android 显示远程图片
    C#引用非托管.dll
    c# 反射
    storyboard 中tableview 中的cell 手动更改高度 报错的解决办法。
    我也来开了博客记录我的开发之路
    openlayers点击地图图标,图标跳动 动画Demo实现 (复制内容至html文件可查看效果)
    element 分页多选表格换页时保留勾选数据---reverse-selection
    表单输入不显示,input弹窗选择不带回,角色不同样式差异等问题,可能的思路
  • 原文地址:https://www.cnblogs.com/graph/p/3027586.html
Copyright © 2011-2022 走看看