From ea0e63969583ee340924ee6bfb8cbb8b6688c23e Mon Sep 17 00:00:00 2001 From: weikunkun Date: Tue, 11 May 2021 19:00:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A00001.=E4=B8=A4=E6=95=B0?= =?UTF-8?q?=E4=B9=8B=E5=92=8C=20Java=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0001.两数之和.md | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/problems/0001.两数之和.md b/problems/0001.两数之和.md index 5a74b919..7425ff4f 100644 --- a/problems/0001.两数之和.md +++ b/problems/0001.两数之和.md @@ -85,7 +85,24 @@ public: Java: - +```java +public int[] twoSum(int[] nums, int target) { + int[] res = new int[2]; + if(nums == null || nums.length == 0){ + return res; + } + Map map = new HashMap<>(); + for(int i = 0; i < nums.length; i++){ + int temp = target - nums[i]; + if(map.containsKey(temp)){ + res[1] = i; + res[0] = map.get(temp); + } + map.put(nums[i], i); + } + return res; +} +``` Python: