力扣热门100题刷题笔记 - 1.两数之和

力扣热门100题 - 1.两数之和

题目链接:1.两数之和

题目描述:

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

你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。

你可以按任意顺序返回答案。

示例:

输入:nums = [2,7,11,15], target = 9
输出:[0,1]
解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。
输入:nums = [3,2,4], target = 6
输出:[1,2]
输入:nums = [3,3], target = 6
输出:[0,1]

提示:

2 <= nums.length <= 104
-109 <= nums[i] <= 109
-109 <= target <= 109
只会存在一个有效答案

解题思路一 暴力破解:

使用两个for循环进行枚举
数组中同一个元素在答案里不能重复出现,所以第二层for循环从i+1开始

时间复杂度:O(N^2)

代码:

class Solution {
    public int[] twoSum(int[] nums, int target) {
        int len = nums.length;
        for (int i = 0; i < len; i++) {
            for (int j = i + 1; j < len; j++) {
                if (nums[i] + nums[j] == target){
                    return new int[]{i,j};
                }
            }
        }
        return null;
    }
}

解题思路二 哈希表:

使用哈希表在遍历中记录数据然后比较哈希表中有没有和当前数相加等于target的值
哈希表 key 使用数组的值方便使用 containsKey() 查找符合结果的数
value 使用值的下标 方便返回结果

时间复杂度:O(N)

代码:

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

力扣热门100题刷题笔记 - 1.两数之和_第1张图片

你可能感兴趣的:(力扣刷题记录,leetcode,笔记,算法)