LeetCode - 两数之和
题目信息
源地址:两数之和
给定一个整数数组 nums
和一个整数目标值 target
,请你在该数组中找出和为目标值 target
的那两个整数,并返回它们的数组下标。
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。
你可以按任意顺序返回答案。
提示信息
示例 1
输入:nums = [2,7,11,15], target = 9 输出:[0,1] 解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。
示例 2
输入:nums = [3,2,4], target = 6 输出:[1,2]
示例 3
输入:nums = [3,3], target = 6 输出:[0,1]
限制
2 <= nums.length <= 10^3
-10^9 <= nums[i] <= 10^9
-10^9 <= target <= 10^9
- 只会存在一个有效答案
实现逻辑
暴力枚举
最先想到的逻辑肯定是使用双层循环暴力查找。
当然,采用这种方式的时间复杂度是
package cn.fatedeity.algorithm.leetcode; public class TwoSum { public int[] answer(int[] nums, int target) { for (int i = 0; i < nums.length; i++) { for (int j = i + 1; j < nums.length; j++) { if (nums[i] + nums[j] == target) { return new int[]{i, j}; } } } return new int[0]; } }
哈希匹配
如果采用“空间换时间”的方法,利用哈希表结构查找时间复杂度为
最终,时间复杂度降到
package cn.fatedeity.algorithm.leetcode; import java.util.HashMap; public class TwoSum { public int[] answer(int[] nums, int target) { HashMap<Integer, Integer> hashMap = new HashMap<>(); for (int i = 0; i < nums.length; i++) { int diff = target - nums[i]; if (hashMap.containsKey(diff)) { return new int[]{hashMap.get(diff), i}; } hashMap.put(nums[i], i); } return new int[0]; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了