【leetcode】1.Two Sum

发布时间:2019-06-24 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了【leetcode】1.Two Sum脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
 路出家c++,急需提升c++熟练度,所以开始c++重新刷leetcode。
 注重做题中c++基础的总结~

Two Sum

Given an array of integers, return indices of the two numbers such that they add up to a sPEcific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:
Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

my solution

刚开始没有注意到返回的是数组下标,直接排序了,后来在原基础上修改,直接复制了个原数组,遍历原数组,找结果在原来数组的位置。当然用hashmap代码更简洁。

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        int p1 = 0;
        int p2 = nums.size() - 1;
        vector<int> result;
        bool find_result = false;
        vector<int> old_nums(nums);
        sort(nums.begin(), nums.end());
        while (p1 < p2) {
            int sum = nums[p1] + nums[p2];
            if (sum > target) 
                p2--;
            else if (sum < target)
                p1++;
            else {        
                for (int i = 0; i < old_nums.size() ; i++) {
                    if (old_nums[i] == nums[p1]){
                        result.push_back(i);
                        continue;
                    }
                    if (old_nums[i] == nums[p2]){
                        result.push_back(i);
                        continue;
                    }
                }
                find_result = true;
                break;
            }
        }
    return result;
    }
};

best solution

#即hashmap
vector<int> twoSum(vector<int> &numbers, int target)
{
    //Key is the number and value is its index in the vector.
    unordered_map<int, int> hash;
    vector<int> result;
    for (int i = 0; i < numbers.size(); i++) {
        int numberToFind = target - numbers[i];

            //if numberToFind is found in map, return them
        if (hash.find(numberToFind) != hash.end()) {
                    //+1 because indices are NOT zero based
            result.push_back(hash[numberToFind] + 1);
            result.push_back(i + 1);            
            return result;
        }

            //number was not found. Put it in the map.
        //重载了[]可直接访问,插入。  operator[]在主键不存在时,自动创建
        hash[numbers[i]] = i;
        #hash.insert(pair<int, int>(numbers[i], i));
    }
    return result;
}
class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        unordered_map<int, int> lookup;
        for (int i = 0; i < nums.size(); ++i) {
            //使用count代替find
            if (lookup.count(target - nums[i])) {
                return {lookup[target - nums[i]], i};
            }
            lookup[nums[i]] = i;
        }
        return {};
    }
};

point

  • vector排序:
#include<algorithm>
sort(v.begin(),v.end());
  • vector 拷贝
vector<int> list;
#拷贝构造函数
vector<int> tem1(list);
#assign
vector<int> tem2;
tem2.assign(list.begin(), list.end());
#insert
vector<int> tem3;
tem3.insert(tem3.end(), list.begin(), list.end());
  • unorderd_map

wiki:http://classfoo.com/ccby/arti...
与map区别在于 map会根据key进行排序:https://www.cnblogs.com/NeilZ...

脚本宝典总结

以上是脚本宝典为你收集整理的【leetcode】1.Two Sum全部内容,希望文章能够帮你解决【leetcode】1.Two Sum所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。