1.两数之和
📆Date: 2023年3月13日
✒️Author: SmartBoy
📓Classify:LeetCode
🔖Language: Java
✨题目描述:
给定一个整数数组
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 <= 104
-109 <= nums[i] <= 109
-109 <= target <= 109
- 只会存在一个有效答案
**进阶:**你可以想出一个时间复杂度小于
O(n2)
的算法吗?
🎉解题思路:
可以选择暴力穷举的方法或HashMap检索的方法
详细见代码
🥇Java源码:
package LeetCodePro.Problem3; import java.util.Arrays; import java.util.Scanner; /** * @Auther:Xust_SmartBoy * @Date:2023/3/13 20:39 * @Description:LeetCodePro.Problem3 * @User:SmartBoy * @VERSON: * 给定一个整数数组 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 <= 104 * -109 <= nums[i] <= 109 * -109 <= target <= 109 * 只会存在一个有效答案 * * * 进阶:你可以想出一个时间复杂度小于 O(n2) 的算法吗? */ public class Problem3 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入数组的长度:"); int n = scanner.nextInt(); System.out.println("请输入一个整数数组:"); int[] nums = new int[n]; for (int i = 0; i < n; i++) { nums[i] = scanner.nextInt(); } System.out.println("请输入目标值:"); int target = scanner.nextInt(); Solution1 solution1 = new Solution1(); Solution2 solution2 = new Solution2(); Solution3 solution3 = new Solution3(); int[] result1 = solution1.twoSum(nums,target); int[] result2 = solution2.twoSum(nums,target); int[] result3 = solution3.twoSum(nums,target); System.out.println(Arrays.toString(result3)); } }
package LeetCodePro.Problem3; /** * @Auther:Xust_SmartBoy * @Date:2023/3/13 19:17 * @Description:LeetCodePro.Problem1 * @User:SmartBoy * @VERSON:暴力穷举 */ public class Solution1 { public int[] twoSum(int[] nums, int target) { int[] result = new int[2]; for (int i = 0; i < nums.length; i++) { for (int j = i + 1; j < nums.length; j++) { if (nums[i] + nums[j] == target){ result[0] = i; result[1] = j; } } } return result; } }
package LeetCodePro.Problem3; import java.util.HashMap; import java.util.Map; /** * @Auther:Xust_SmartBoy * @Date:2023/3/13 19:33 * @Description:LeetCodePro.Problem1 * @User:SmartBoy * @VERSON:HashMap检索 */ public class Solution2 { public int[] twoSum(int[] nums, int target) { /*key 为元素值,value 为每个元素对应的下标*/ Map<Integer,Integer> map = new HashMap<>(); int[] result = new int[2]; // 定义返回数组 for (int i = 0; i < nums.length; i++) { int another = target - nums[i]; // 用another存储目标值与所指数组值的插值 Integer anotherIndex = map.get(another); // 得到插值的HashMap的key if (anotherIndex != null){ // key位置的元素已存在 result[0] = anotherIndex; result[1] = i; break; } else map.put(nums[i],i); // key位置元素为空,将数组i位置元素值存入key,下标存为value } return result; } }
package LeetCodePro.Problem3; import java.util.HashMap; import java.util.Map; /** * @Auther:Xust_SmartBoy * @Date:2023/3/13 19:38 * @Description:LeetCodePro.Problem1 * @User:SmartBoy * @VERSON: */ public class Solution3 { public int[] twoSum(int[] nums, int target) { Map<Integer,Integer> map = new HashMap<>(); int[] result = new int[2]; for (int i = 0; i < nums.length; i++) { if (map.containsKey(target - nums[i])){ result[0] = map.get(target - nums[i]); result[1] = i; } map.put(nums[i],i); } return result; } }