[LeetCode]1. 两数之和

题目描述:

给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。

你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。

示例:

给定 nums = [2, 7, 11, 15], target = 9

因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]

思路:

我们应该首先想用暴力求解的话,怎么做?

我们会遍历数组a,然后看除了a数组中有没有target-a的数,这样就能保证该数组有两个数和等于target;但是时间复杂度为(O(n^2));

接下来,想一想如何更快一点呢?

对,我们可以借用哈希(python叫字典),我们遍历元素的时候,且记录元素的下标,当我们找target-a时候,只需要在字典找,就可以了,查找字典时间复杂度为(O(1))

所以,

时间复杂度:(O(n))

空间复杂度:(O(n))

代码:

Python版

class Solution:
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        n = len(nums)
        lookup = {}
        for i in range(n):
            tmp = target - nums[i]
            if tmp in lookup:
                return [lookup[tmp], i]
            lookup[nums[i]] = i

C++ 版

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        unordered_map<int,int> lookup;
        vector<int> res;
        for(int i = 0; i < nums.size(); i++)
        {
            int num_to_find = target - nums[i];
            if(lookup.find(num_to_find) != lookup.end()) 
            {
                res.push_back(lookup[num_to_find]);
                res.push_back(i);
                return res ;     
            }
            lookup[nums[i]] = i;
        }
        return res;
        
    }
};

java版

class Solution {
    public int[] twoSum(int[] nums, int target) {
        int[] result = new int[2];
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        for (int i = 0; i < nums.length; i++){
            if (map.containsKey(target - nums[i])){
                result[1] = i ;
                result[0] = map.get(target - nums[i]);
                return result;
            }
            map.put(nums[i],i);
        }
        return result;
        
    }
}

大家可以关注我的知乎专栏,你的关注是我变强的动力.

并向我提出宝贵的建议

原文地址:https://www.cnblogs.com/powercai/p/10690539.html