Skip to content

Commit 4828b21

Browse files
committedMar 6, 2025·
二刷1
1 parent 684679d commit 4828b21

File tree

3 files changed

+83
-11
lines changed

3 files changed

+83
-11
lines changed
 

‎docs/0001-two-sum.adoc

+53-11
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,67 @@
11
[#0001-two-sum]
2-
= 1. Two Sum
2+
= 1. 两数之和
33

4-
{leetcode}/problems/two-sum/[LeetCode - Two Sum^]
4+
https://leetcode.cn/problems/two-sum/[LeetCode - 1. 两数之和 ^]
55

6-
Given an array of integers, return *indices* of the two numbers such that they add up to a specific target.
6+
给定一个整数数组 `nums` 和一个整数目标值 `target`,请你在该数组中找出 *和为目标值* `target` 的那 *两个* 整数,并返回它们的数组下标。
77

8-
You may assume that each input would have *_exactly_* one solution, and you may not use the _same_ element twice.
8+
你可以假设每种输入只会对应一个答案,并且你不能使用两次相同的元素。
99

10-
.Example:
11-
[subs="verbatim,quotes,macros"]
12-
----
13-
Given nums = [2, 7, 11, 15], target = 9,
10+
你可以按任意顺序返回答案。
1411

15-
Because nums[*0*] + nums[*1*] = 2 + 7 = 9,
16-
return [*0*, *1*].
17-
----
12+
*示例 1:*
13+
14+
....
15+
输入:nums = [2,7,11,15], target = 9
16+
输出:[0,1]
17+
解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。
18+
....
19+
20+
*示例 2:*
21+
22+
....
23+
输入:nums = [3,2,4], target = 6
24+
输出:[1,2]
25+
....
26+
27+
*示例 3:*
28+
29+
....
30+
输入:nums = [3,3], target = 6
31+
输出:[0,1]
32+
....
33+
34+
*提示:*
35+
36+
* `+2 <= nums.length <= 10+`^`4`^
37+
* stem:[-10^9 <= "nums"[i\] <= 10^9]
38+
* stem:[-10^9 <= "target" <= 10^9]
39+
* *只会存在一个有效答案*
40+
41+
**进阶:**你可以想出一个时间复杂度小于 stem:[O(n^2)] 的算法吗?
42+
43+
== 解题思路
1844

1945

2046
[[src-0001]]
47+
[tabs]
48+
====
49+
一刷::
50+
+
51+
--
2152
[{java_src_attr}]
2253
----
2354
include::{sourcedir}/_0001_TwoSum.java[tag=answer]
2455
----
56+
--
57+
58+
二刷::
59+
+
60+
--
61+
[{java_src_attr}]
62+
----
63+
include::{sourcedir}/_0001_TwoSum_2.java[tag=answer]
64+
----
65+
--
66+
====
2567

‎logbook/202503.adoc

+5
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ endif::[]
3535
|{doc_base_url}/0081-search-in-rotated-sorted-array-ii.adoc[题解]
3636
|✅ 关注有序区间,确定目标值在有序区间内,则在有序区间查找;反之,则在另外一部分内查找。另外,通过移动一个指针即可避开重复元素。
3737

38+
|{counter:codes2503}
39+
|{leetcode_base_url}/two-sum/[1. 两数之和^]
40+
|{doc_base_url}/0001-two-sum.adoc[题解]
41+
|✅ 注意审题!返回的是数组下标。
42+
3843
|===
3944

4045
截止目前,本轮练习一共完成 {codes2503} 道题。
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class _0001_TwoSum_2 {
7+
// tag::answer[]
8+
/**
9+
* @author D瓜哥 · https://www.diguage.com
10+
* @since 2025-03-06 16:36:45
11+
*/
12+
public int[] twoSum(int[] nums, int target) {
13+
Map<Integer, Integer> numToIdx = new HashMap<>();
14+
for (int i = 0; i < nums.length; i++) {
15+
int num = nums[i];
16+
if (numToIdx.containsKey(target - num)) {
17+
return new int[]{numToIdx.get(target - num), i};
18+
} else {
19+
numToIdx.put(num, i);
20+
}
21+
}
22+
return null;
23+
}
24+
// end::answer[]
25+
}

0 commit comments

Comments
 (0)
Please sign in to comment.