1. 程式人生 > >LeetCode1 二數和 2018.11.22_10.39

LeetCode1 二數和 2018.11.22_10.39

//2018.11.22_10.39
class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        unordered_map<int,int> hashmap;
        vector<int> vt;
        for(int i=0;i<nums.size();i++){
            hashmap[nums[i]]=i;
        }
        for(int i=0;i<
nums.size();i++){ if(hashmap[target-nums[i]]!=hashmap.end()&&hashmap[target-nums[i]]!=i]){ vt.push_back(i); vt.push_back(hashmap[target-nums[i]]); return vt; } } } };
2018.11.22_11.01
class Solution {
public: vector<int> twoSum(vector<int>& nums, int target) { unordered_map<int,int> hashmap; vector<int> vt; for(int i=0;i<nums.size();i++){ hashmap.insert(pair<int,int>(nums[i],i)); } for(int i=0;i<nums.size
();i++){ if(hashmap.find(target-nums[i])!=hashmap.end()&&hashmap[target-nums[i]]!=i){ vt.push_back(i); vt.push_back(hashmap[target-nums[i]]); return vt; } } } };