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
    //先对numbers数组排序,然后利用左右两个指针查找,注意原有数组的索引,时间复杂度O(nlogn)
    #include <iostream> #include <vector> #include <algorithm> #include <map> #include <iterator> using namespace std; vector<int> twoSum(vector<int> &numbers, int target){ vector<int> numberCopy=numbers; sort(numberCopy.begin(),numberCopy.end()); int left = 0, right = numberCopy.size()-1; vector<int> res; while(left < right){ if(numberCopy[left]+numberCopy[right] > target) right--; else if(numberCopy[left]+numberCopy[right] < target) left++; else { vector<int>::iterator firstIndex = find(numbers.begin(),numbers.end(),numberCopy[left]); int first = distance(numbers.begin(),firstIndex); vector<int>::iterator secondIndex = find(numbers.begin(), numbers.end(),numberCopy[right]); if(secondIndex == firstIndex) secondIndex = find(secondIndex+1, numbers.end(),numberCopy[right]); int second = distance(numbers.begin(),secondIndex); if(first > second) swap(first,second); res.push_back(first+1); res.push_back(second+1); break; } } return res; } int main(){ vector<int> numbers; numbers.push_back(0); numbers.push_back(4); numbers.push_back(3); numbers.push_back(0); vector<int> res = twoSum(numbers,0); cout<<res[0]<<" "<<res[1]<<endl; }
    /*
     *利用map存储,由于map的find函数的时间复杂度是O(logn),故总体的时间复杂度是O(nlogn),
     *可以将map换成hashMap,hashMap的查找时间复杂度为O(1),故整体时间复杂度为O(n)
     *由于C++98不支持hashMap可以换成java的hashMap
     *也可以利用C++11的unordered_map,其是c++的hashMap
     */
     
    #include <iostream>
    #include <vector>
    #include <map>
    
    using namespace std;
    
    vector<int> twoSum(vector<int> &numbers, int target){
        map<int,int> twoSumMap;
        vector<int> res(2);
        for(int i = 0 ; i <  numbers.size(); ++ i){
            if(twoSumMap.find(numbers[i])!= twoSumMap.end()){
                res[0] = twoSumMap[numbers[i]]+1;
                res[1] = i + 1;
                break;
            }else{
                twoSumMap.insert(make_pair(target-numbers[i],i));
            }
        }
        return res;
    }

    利用C++11unordered_map,时间复杂度O(n)

    #include <iostream>
    #include <vector>
    #include <unordered_map>
    
    using namespace std;
    
    vector<int> twoSum(vector<int> &numbers, int target){
        unordered_map<int,int> twoSumMap;
        vector<int> res(2);
        for(int i = 0 ; i <  numbers.size(); ++ i){
            if(twoSumMap.find(numbers[i])!= twoSumMap.end()){
                res[0] = twoSumMap[numbers[i]]+1;
                res[1] = i + 1;
                break;
            }else{
                twoSumMap.insert(make_pair(target-numbers[i],i));
            }
        }
        return res;
    }
  • 相关阅读:
    hihoCoder #1062 : 最近公共祖先·一
    hihoCoder #1050 : 树中的最长路
    hihoCoder #1049 : 后序遍历
    108 Convert Sorted Array to Binary Search Tree 将有序数组转换为二叉搜索树
    107 Binary Tree Level Order Traversal II 二叉树的层次遍历 II
    106 Construct Binary Tree from Inorder and Postorder Traversal 从中序与后序遍历序列构造二叉树
    105 Construct Binary Tree from Preorder and Inorder Traversal 从前序与中序遍历序列构造二叉树
    104 Maximum Depth of Binary Tree 二叉树的最大深度
    102 Binary Tree Level Order Traversal 二叉树的层次遍历
    101 Symmetric Tree 判断一颗二叉树是否是镜像二叉树
  • 原文地址:https://www.cnblogs.com/xiongqiangcs/p/3620024.html
Copyright © 2011-2022 走看看