Skip to content

Commit 9e670f4

Browse files
committed
四刷167
1 parent 04de196 commit 9e670f4

File tree

3 files changed

+79
-13
lines changed

3 files changed

+79
-13
lines changed

docs/0167-two-sum-ii-input-array-is-sorted.adoc

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,49 @@
11
[#0167-two-sum-ii-input-array-is-sorted]
2-
= 167. Two Sum II - Input array is sorted
2+
= 167. 两数之和 II - 输入有序数组
33

4-
{leetcode}/problems/two-sum-ii-input-array-is-sorted/[LeetCode - Two Sum II - Input array is sorted^]
4+
https://leetcode.cn/problems/two-sum-ii-input-array-is-sorted/[LeetCode - 167. 两数之和 II - 输入有序数组 ^]
55

6-
Given an array of integers that is already **sorted in ascending order**, find two numbers such that they add up to a specific target number.
6+
给你一个下标从 *1* 开始的整数数组 `numbers` ,该数组已按 *非递减顺序排列* ,请你从数组中找出满足相加之和等于目标数 `target` 的两个数。如果设这两个数分别是 `+numbers[index~1~]+``+numbers[index~2~]+` ,则 `+1 <= index~1~ < index~2~ <= numbers.length+`
77

8-
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2.
8+
以长度为 2 的整数数组 `+[index~1~, index~2~]+` 的形式返回这两个整数的下标 `index~1~``index~2~`
99

10-
*Note:*
10+
你可以假设每个输入 *只对应唯一的答案* ,而且你 *不可以* 重复使用相同的元素。
1111

12-
* Your returned answers (both index1 and index2) are not zero-based.
13-
* You may assume that each input would have *exactly* one solution and you may not use the *same* element twice.
12+
你所设计的解决方案必须只使用常量级的额外空间。
13+
14+
*示例 1:*
15+
16+
....
17+
输入:numbers = [2,7,11,15], target = 9
18+
输出:[1,2]
19+
解释:2 与 7 之和等于目标数 9 。因此 index1 = 1, index2 = 2 。返回 [1, 2] 。
20+
....
21+
22+
*示例 2:*
23+
24+
....
25+
输入:numbers = [2,3,4], target = 6
26+
输出:[1,3]
27+
解释:2 与 4 之和等于目标数 6 。因此 index1 = 1, index2 = 3 。返回 [1, 3] 。
28+
....
29+
30+
*示例 3:*
31+
32+
....
33+
输入:numbers = [-1,0], target = -1
34+
输出:[1,2]
35+
解释:-1 与 0 之和等于目标数 -1 。因此 index1 = 1, index2 = 2 。返回 [1, 2] 。
36+
....
37+
38+
39+
*提示:*
40+
41+
* `2 \<= numbers.length \<= 3 * 10^4^`
42+
* `+-1000 <= numbers[i] <= 1000+`
43+
* `numbers`*非递减顺序* 排列
44+
* `+-1000 <= target <= 1000+`
45+
* *仅存在一个有效答案*
1446
15-
.Example:
16-
----
17-
Input: numbers = [2,7,11,15], target = 9
18-
Output: [1,2]
19-
Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2.
20-
----
2147
2248
== 思路分析
2349

@@ -62,8 +88,18 @@ include::{sourcedir}/_0167_TwoSumIIInputArrayIsSorted_2.java[tag=answer]
6288
include::{sourcedir}/_0167_TwoSumIIInputArrayIsSorted_3.java[tag=answer]
6389
----
6490
--
91+
92+
四刷::
93+
+
94+
--
95+
[{java_src_attr}]
96+
----
97+
include::{sourcedir}/_0167_TwoSumIiInputArrayIsSorted_4.java[tag=answer]
98+
----
99+
--
65100
====
66101

102+
67103
== 参考资料
68104

69105
. https://leetcode.cn/problems/two-sum-ii-input-array-is-sorted/solutions/337156/liang-shu-zhi-he-ii-shu-ru-you-xu-shu-zu-by-leet-2/[167. 两数之和 II - 输入有序数组 - 官方题解^]

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,11 @@ endif::[]
543543
|{doc_base_url}/0670-maximum-swap.adoc[题解]
544544
|✅ 贪心算法。最高位尽可能跟后面的最大数字进行交换。
545545

546+
|{counter:codes2503}
547+
|{leetcode_base_url}/two-sum-ii-input-array-is-sorted/[167. Two Sum II - Input array is sorted^]
548+
|{doc_base_url}/0167-two-sum-ii-input-array-is-sorted.adoc[题解]
549+
|✅ 双指针
550+
546551
|===
547552

548553
截止目前,本轮练习一共完成 {codes2503} 道题。
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0167_TwoSumIiInputArrayIsSorted_4 {
4+
// tag::answer[]
5+
6+
/**
7+
* @author D瓜哥 · https://www.diguage.com
8+
* @since 2025-04-29 08:30
9+
*/
10+
public int[] twoSum(int[] numbers, int target) {
11+
int left = 0, right = numbers.length - 1;
12+
while (left < right) {
13+
int sum = numbers[left] + numbers[right];
14+
if (sum == target) {
15+
break;
16+
} else if (sum < target) {
17+
left++;
18+
} else {
19+
right--;
20+
}
21+
}
22+
return new int[]{left + 1, right + 1};
23+
}
24+
// end::answer[]
25+
}

0 commit comments

Comments
 (0)